<?php

namespace app\erp\model;

use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use app\jun\controller\Api;
use app\supplier\controller\Buildtpl;
use Darabonba\OpenApi\Models\Config;
use think\App;
use think\Cache;
use think\Db;
use think\Model;
use traits\model\SoftDelete;

class Orderdb extends Model
{
    use SoftDelete;

    protected $deleteTime = 'delete_time';
    protected $pk = 'order_id';
    protected $name = 'order';
    public $last_order_no = '';
    protected $type = [
        'create_time' => 'timestamp:Y-m-d H:i',
        'delete_time' => 'timestamp:Y-m-d',
        'confirm_time' => 'timestamp:Y-m-d H:i',
        'shopping_time' => 'timestamp:Y-m-d H:i',
        'receipt_time' => 'timestamp:Y-m-d H:i',
        'cancel_time' => 'timestamp:Y-m-d H:i',
        'over_time' => 'timestamp:Y-m-d H:i',
        'pay_time' => 'timestamp:Y-m-d H:i',
        'supplier_confirm_time' => 'timestamp:Y-m-d H:i',
        'push_supplier_time' => 'timestamp:Y-m-d H:i',
        'caigou_invoice_time' => 'timestamp:Y-m-d H:i',
        'gys_sh_time' => 'timestamp:Y-m-d H:i',
        'invoice_time' => 'timestamp:Y-m-d H:i',
        'apply_time' => 'timestamp:Y-m-d H:i',
        'apply_shehe_time' => 'timestamp:Y-m-d H:i',
        'ruku_time' => 'timestamp:Y-m-d H:i',
        'ghs_pay_time' => 'timestamp:Y-m-d H:i',
        'gys_fh_time' => 'timestamp:Y-m-d H:i'
    ];
    // 自动完成时间转换
    protected $autoWriteTimestamp = true;
    protected $insert = [
        'partner_id' => PARTNER_ID,
        'site_id',
        'order_no',
        'shopping_state' => 1,
        'order_member_id' => MEMBER_ID,
        'pay_state' => 1,
        'purchase_state' => 1,
        'entry_state' => 1,
        'out_state' => 1,
        'cost_state' => 1,
        'invoice_state' => 1,
        'receipt_state' => 1,
        'judge_state' => 1
    ];

    protected $append = ['ZhekouMoney'];

    /*public function getDingyueAttr($value, $row)
    {
        if ($row['wuliu'] != '自配物流') {
            if ($row['gys_fh_state'] == 1) {
                $chk_result = Db::name('kdresult')->where('order_id', $row['order_id'])->count();
                if ($chk_result) {
                    $result = 1; // 成功的
                } else {
                    $result = 2; // 失败的
                }
            } else {
                $result = 3; // 未配送
            }
        } else {
            $result = 4; // 自配物流
        }
        return $result;
    }*/


    protected function setSiteIdAttr($value)
    {
        if (!$value) {
            return SITE_ID;
        }
        if ($value == 0) {
            return null;
        }
        return $value;
    }

    // 自动完成订单号
    protected function setOrderNoAttr()
    {
        if (isset ($this->param ['out_order_no'])) {
            if (isset ($this->param ['is_jingjia'])) {
                return 'JJ' . $this->param ['out_order_no'];
            } else {
                return 'CS' . $this->param ['out_order_no'];
            }
        } else {
            return 'TLJ' . base_convert(uniqid(), 16, 10);
        }
    }

    public $param = array();

    protected function cache_name($name)
    {
        $new_arr = $this->param;
        $new_arr ['partner_id'] = PARTNER_ID;
        if (!isset ($new_arr ['site_id'])) {
            $new_arr ['site_id'] = SITE_ID;
        }
        ksort($new_arr);
        $param_str = http_build_query($new_arr);
        return md5($name . $param_str);
    }
    // 发送短信
    /*
     * 新订单通知：SMS_225925355 {新订单提醒}-您好，军网平台有新的订单，单号：${order}，金额：${money}。地址：${address}。请及时处理。
     * 到账通知：SMS_225910508 {客户款到账提醒}-尊敬的合作伙伴您好，军网平台订单，单号：${order}，金额：${money}。客户款已到账，请及时查看，如有疑问可点击系统质疑按钮。
     * 付款通知：SMS_225935234 {货款支付提醒}-尊敬的合作伙伴您好，军网平台订单，单号：${order}，金额：${money}。货款已支付，请及时查看，如有疑问可点击系统质疑按钮。
     * 删除:SMS_229610853 有个单号
     */
    public function sendMsg($phone, $tpl_code, $param)
    {
        $post_json = json_encode($param);
        $client = self::createClient();
        $sendSmsRequest = new SendSmsRequest ([
            "signName" => "泰龙吉",
            "phoneNumbers" => $phone,
            "templateCode" => $tpl_code,
            "templateParam" => $post_json
        ]);
        $return = $client->sendSms($sendSmsRequest);
        return $return;
        // dump($return);
    }

    public static function createClient()
    {
        $accessKeyId = 'LTAIeRWUxR6rp7sj';
        $accessKeySecret = 'n4qEWLCengJ012UAWl7UJvMOVMLgXY';
        $config = new Config ([
            "accessKeyId" => $accessKeyId,
            "accessKeySecret" => $accessKeySecret
        ]);
        // 访问的域名
        $config->endpoint = "dysmsapi.aliyuncs.com";
        return new Dysmsapi ($config);
    }

    public function _emptyParam()
    {
        $this->param = array();
    }

    public function scopeOrderId($query, $order_id)
    {
        $query->where('order_id', $order_id);
    }

    public function scopeOrderNo($query, $order_no)
    {
        if (is_array($order_no)) {
            $query->where('order_no', 'IN', $order_no);
        } else {
            $query->where('order_no', 'like', '%' . $order_no . '%');
        }
    }

    public function scopePayState($query, $pay_state)
    {
        $query->where('pay_state', 'IN', $pay_state);
    }

    public function scopeRuKu($query, $ruku)
    {
        $query->where('ruku', 'IN', $ruku);
    }

    public function scopeOrderState($query, $order_state)
    {
        $query->where('order_state', 'IN', $order_state);
    }

    public function scopeMemberId($query, $member_id)
    {
        $query->where('member_id', $member_id);
    }

    public function scopeJudgeState($query, $judge_state)
    {
        $query->where('judge_state', 'IN', $judge_state);
    }

    public function scopeSusername($query, $susername)
    {
        $memberdb = new Memberdb ();
        $get_supplier_id = $memberdb->useGlobalScope(false)->where('supplier_name', 'like', '%' . $susername . '%')->where('is_supplier', 1)->select();
        if (!$get_supplier_id->isEmpty()) {
            $member_ids = array();
            $get_supplier_id = $get_supplier_id->toArray();
            foreach ($get_supplier_id as $key => $val) {
                array_push($member_ids, $val ['member_id']);
            }
            $query->where('supplier_id', 'IN', $member_ids);
        } else {
            $query->where('supplier_id', -1);
        }
    }

    // 精准搜索
    public function scopeSusernameJz($query, $susername)
    {
        $memberdb = new Memberdb ();
        $get_supplier_id = $memberdb->useGlobalScope(false)->where('supplier_name', $susername)->where('is_supplier', 1)->select();
        if (!$get_supplier_id->isEmpty()) {
            $member_ids = array();
            $get_supplier_id = $get_supplier_id->toArray();
            foreach ($get_supplier_id as $key => $val) {
                array_push($member_ids, $val ['member_id']);
            }
            $query->where('supplier_id', 'IN', $member_ids);
        } else {
            $query->where('supplier_id', -1);
        }
    }

    public function scopeYewuyuan($query, $yewuyuan)
    {
        $memberdb = new Memberdb ();
        $get_supplier_id = $memberdb->useGlobalScope(false)->where('yewuyuan', 'like', '%' . $yewuyuan . '%')->where('is_supplier', 1)->select();
        if (!$get_supplier_id->isEmpty()) {
            $member_ids = array();
            $get_supplier_id = $get_supplier_id->toArray();
            foreach ($get_supplier_id as $key => $val) {
                array_push($member_ids, $val ['member_id']);
            }
            $query->where('supplier_id', 'IN', $member_ids);
        } else {
            $query->where('supplier_id', -1);
        }
    }

    public function scopeZhuli($query, $zhuli)
    {
        $memberdb = new Memberdb ();
        $get_supplier_id = $memberdb->useGlobalScope(false)->where('zhuli', 'like', '%' . $zhuli . '%')->where('is_supplier', 1)->select();
        if (!$get_supplier_id->isEmpty()) {
            $member_ids = array();
            $get_supplier_id = $get_supplier_id->toArray();
            foreach ($get_supplier_id as $key => $val) {
                array_push($member_ids, $val ['member_id']);
            }
            $query->where('supplier_id', 'IN', $member_ids);
        } else {
            $query->where('supplier_id', -1);
        }
    }

    public function scopeNeiqin($query, $neiqin)
    {
        $memberdb = new Memberdb ();
        $get_supplier_id = $memberdb->useGlobalScope(false)->where('neiqin', 'like', '%' . $neiqin . '%')->where('is_supplier', 1)->select();
        if (!$get_supplier_id->isEmpty()) {
            $member_ids = array();
            $get_supplier_id = $get_supplier_id->toArray();
            foreach ($get_supplier_id as $key => $val) {
                array_push($member_ids, $val ['member_id']);
            }
            $query->where('supplier_id', 'IN', $member_ids);
        } else {
            $query->where('supplier_id', -1);
        }
    }

    public function scopeChuliren($query, $chuliren)
    {
        $memberdb = new Memberdb ();
        $get_supplier_id = $memberdb->useGlobalScope(false)->where('chuliren', 'like', '%' . $chuliren . '%')->where('is_supplier', 1)->select();
        if (!$get_supplier_id->isEmpty()) {
            $member_ids = array();
            $get_supplier_id = $get_supplier_id->toArray();
            foreach ($get_supplier_id as $key => $val) {
                array_push($member_ids, $val ['member_id']);
            }
            $query->where('supplier_id', 'IN', $member_ids);
        } else {
            $query->where('supplier_id', -1);
        }
    }

    public function scopeUsername($query, $username)
    {
        $memberdb = new Memberdb ();
        $memberdb->param ['username_like'] = $username;
        $memberdb->param ['_o'] = 'member_id';
        $memberdb->param ['_b'] = 'asc';
        $memberdb->param ['page'] = 1;
        $memberdb->param ['limit'] = 100;
        $data = $memberdb->ajaxSelect();
        if ($data ['count'] > 0) {
            $member_ids = array();
            foreach ($data ['data'] as $key => $val) {
                array_push($member_ids, $val ['member_id']);
            }
            $query->where('member_id', 'IN', $member_ids);
        } else {
            $query->where('member_id', -1);
        }
    }

    public function scopeCreateTimeStart($query, $create_time)
    {
        $query->whereTime('create_time', '>=', $create_time);
    }

    public function scopeCreateTimeEnd($query, $create_time)
    {
        $time = strtotime($create_time);
        $date = strtotime('+1 day', $time);
        $query->whereTime('create_time', '<', $date);
    }

    public function scopeInvoiceTimeStart($query, $invoice_time)
    {
        $query->whereTime('invoice_time', '>=', $invoice_time);
    }

    public function scopeInvoiceTimeEnd($query, $invoice_time)
    {
        $query->whereTime('invoice_time', '<', $invoice_time);
    }

    public function scopeApplyTimeStart($query, $apply_time)
    {
        $query->whereTime('apply_time', '>=', $apply_time);
    }

    public function scopeApplyTimeEnd($query, $apply_time)
    {
        $query->whereTime('apply_time', '<', $apply_time);
    }

    public function scopeApplySheheTimeStart($query, $apply_shehe_time)
    {
        $query->whereTime('apply_shehe_time', '>=', $apply_shehe_time);
    }

    public function scopeApplySheheTimeEnd($query, $apply_shehe_time)
    {
        $query->whereTime('apply_shehe_time', '<', $apply_shehe_time);
    }

    public function scopeConfirmTimeStart($query, $confirm_time)
    {
        $query->whereTime('confirm_time', '>=', $confirm_time);
    }

    public function scopeConfirmTimeEnd($query, $confirm_time)
    {
        $query->whereTime('confirm_time', '<', $confirm_time);
    }

    public function scopePayTimeStart($query, $pay_time)
    {
        $query->whereTime('pay_time', '>=', $pay_time);
    }

    public function scopePayTimeEnd($query, $pay_time)
    {
        $query->whereTime('pay_time', '<', $pay_time);
    }

    public function scopeDeliveryDate($query, $delivery_date)
    {
        $query->where('delivery_date', $delivery_date);
    }

    public function scopePayType($query, $pay_type)
    {
        $query->where('pay_type', $pay_type);
    }

    public function scopePayNo($query, $pay_type)
    {
        $query->where('pay_no', '%' . $pay_type . '%');
    }

    public function scopeShoppingTimeStart($query, $shopping_time)
    {
        $query->whereTime('shopping_time', '>=', $shopping_time);
    }

    public function scopeShoppingTimeEnd($query, $shopping_time)
    {
        $query->whereTime('shopping_time', '<', $shopping_time);
    }

    public function scopeShoppingState($query, $shopping_state)
    {
        $query->where('shopping_state', 'IN', $shopping_state);
    }

    public function scopeJunOrderType($query, $jun_order_type)
    {
        $query->where('jun_order_type', 'IN', $jun_order_type);
    }

    public function scopeReceiptTimeStart($query, $receipt_time)
    {
        $query->whereTime('receipt_time', '>=', $receipt_time);
    }

    public function scopeOrderMoney($query, $order_money)
    {
        $query->whereTime('order_money', '>=', $order_money);
    }

    public function scopeReceiptTimeEnd($query, $receipt_time)
    {
        $query->whereTime('receipt_time', '<', $receipt_time);
    }

    public function scopeReceiptState($query, $receipt_state)
    {
        $query->where('receipt_state', 'IN', $receipt_state);
    }

    public function scopePurchaseState($query, $purchase_state)
    {
        $query->where('purchase_state', 'IN', $purchase_state);
    }

    public function scopeCaigouState($query, $caigou_state)
    {
        $query->where('caigou_state', 'IN', $caigou_state);
    }

    public function scopeInvoiceState($query, $invoice_state)
    {
        $query->where('invoice_state', 'IN', $invoice_state);
    }

    public function scopeIsInvoice($query, $is_invoice)
    {
        $query->where('is_invoice', $is_invoice);
    }

    public function scopeOutOrderNo($query, $out_order_no)
    {
        if (is_array($out_order_no)) {
            $query->whereIn('out_order_no', $out_order_no);
        } else {
            $query->where('out_order_no', 'like', '%' . $out_order_no . '%');
        }
    }

    public function scopeShoppingId($query, $shopping_id)
    {
        if (is_array($shopping_id)) {
            $query->where('shopping_id', 'IN', $shopping_id);
        } else {
            $query->where('shopping_id', $shopping_id);
        }
    }

    // 基础查询
    public function base($query)
    {
        $field = $this->getDeleteTimeField(true);
        //$query->where('partner_id', PARTNER_ID);
        //$query->where('order_money', '>', 0);
        if ($field) {
            if (isset ($this->param ['delete']) && $this->param ['delete'] == 'yes') {
                $this->onlyTrashed();
            } else {
                $query->useSoftDelete($field);
            }
        }
        // 查看精准搜索
        if (isset($this->param['out_order_no_jz']) && $this->param['out_order_no_jz'] != '') {
            $query->where('out_order_no', $this->param['out_order_no_jz']);
        }
        /*if (isset ($this->param ['site_id'])) {
            if ($this->param ['site_id'] == 0 || $this->param ['site_id'] == '') {
                $query->where('site_id', null);
            } elseif ($this->param ['site_id'] != -1) {
                $query->where('site_id', $this->param ['site_id']);
            }
        } else {
            if (!empty (SITE_ID)) {
                $query->where('site_id', SITE_ID);
            } else {
                $query->where('site_id', null);
            }
        }*/
        if (isset($this->param['jun_id']) && $this->param['jun_id']) {
            $goods_sku = Db::name('goods')->where('jun_id', $this->param['jun_id'])->column('goods_sku');
            if ($goods_sku) {
                $sql = Db::name('cart')->fetchSql(true)->whereIn('goods_sku', $goods_sku)->column('order_no');
                $query->where('order_no', 'exp', 'in(' . $sql . ')');
            } else {
                $query->where('1=2');
            }
        }

        if (isset($this->param['qingkuan_state']) && $this->param['qingkuan_state'] != '') {
            $sql = Db::name('qingkuan')->fetchSql(true)->whereIn('zizhi', 1)->column('order_no');
            if ($this->param['qingkuan_state'] == 1) {
                $query->where('order_no', 'exp', 'in(' . $sql . ')');
            } else {
                $query->where('order_no', 'exp', 'not in(' . $sql . ')');
            }
        }

        // 追价订单
        if (isset($this->param['zhuijia']) && $this->param['zhuijia'] == 'yes') {
            $goods_sql = Db::name('sale_price')->fetchSql(true)->where('caigoujia', '>', 0)->column('goods_sku');
            $cart_sql = Db::name('cart')->fetchSql(true)->whereLike('order_no', 'CS%')->where('goods_sku', 'exp', 'in(' . $goods_sql . ')')->column('order_no');
            $query->where('order_no', 'exp', 'in(' . $cart_sql . ')');
        }
        // 追价订单结束


        // 验证新地址和新手机
        if (isset($this->param['is_new_addr']) && $this->param['is_new_addr'] != '') {
            $query->where('is_new_addr', $this->param['is_new_addr']);
        }
        if (isset($this->param['is_new_phone']) && $this->param['is_new_phone'] != '') {
            $query->where('is_new_phone', $this->param['is_new_phone']);
        }
        // 验证新地址和新手机结束

        if (isset ($this->param ['order_id']) && $this->param ['order_id'] != '') {
            $query->orderId($this->param ['order_id']);
        }
        if (isset ($this->param ['order_no']) && $this->param ['order_no'] != '') {
            $query->orderNo($this->param ['order_no']);
        }
        if (isset ($this->param ['supplier_id']) && $this->param ['supplier_id']) {
            $query->supplierId($this->param ['supplier_id']);
        }

        if (isset($this->param['is_dianzi']) && $this->param['is_dianzi'] != '') {
            $query->where('is_dianzi', $this->param['is_dianzi']);
        }

        if (isset($this->param['api_type']) && in_array($this->param['api_type'], ['82', '026'])) {
            $sql = Db::name('invoice')->fetchSql(true)->where('api_type', $this->param['api_type'])->column('order_nos');
            $query->where('order_no', 'exp', 'in (' . $sql . ')');
        }
        if (isset ($this->param ['member_remark']) && $this->param ['member_remark'] != '') {
            if (isset($this->param['member_remark_type']) && $this->param['member_remark_type'] == 2) {
                $query->memberRemarkNot($this->param ['member_remark']);
            } else {
                $query->memberRemark($this->param ['member_remark']);
            }
        }
        if (isset ($this->param ['remark']) && $this->param ['remark'] != '') {
            if (isset($this->param['remark_type']) && $this->param['remark_type'] == 2) {
                $query->remarkNot($this->param ['remark']);
            } else {
                $query->remark($this->param ['remark']);
            }
        }
        if (isset ($this->param ['suppler_bz']) && $this->param ['suppler_bz'] != '') {
            if (isset($this->param['suppler_bz_type']) && $this->param['suppler_bz_type'] == 2) {
                $query->supplerBzNot($this->param ['suppler_bz']);
            } else {
                $query->supplerBz($this->param ['suppler_bz']);
            }
        }
        if (isset ($this->param ['suppler_bz2']) && $this->param ['suppler_bz2'] != '') {
            $query->supplerBz2($this->param ['suppler_bz2']);
        }
        if (isset ($this->param ['suppler_bz3']) && $this->param ['suppler_bz3'] != '') {
            $query->supplerBz3($this->param ['suppler_bz3']);
        }
        if (isset ($this->param ['pay_state']) && $this->param ['pay_state']) {
            $query->payState($this->param ['pay_state']);
        }
        if (isset ($this->param ['ruku']) && $this->param ['ruku']) {
            $query->ruKu($this->param ['ruku']);
        }
        if (isset ($this->param ['order_state']) && $this->param ['order_state']) {
            $query->orderState($this->param ['order_state']);
        }
        if (isset ($this->param ['member_id']) && $this->param ['member_id']) {
            $query->memberid($this->param ['member_id']);
        }
        if (isset ($this->param ['judge_state']) && $this->param ['judge_state']) {
            $query->judgeState($this->param ['judge_state']);
        }
        if (isset ($this->param ['username']) && $this->param ['username']) {
            $query->username($this->param ['username']);
        }
        if (isset ($this->param ['create_time_start']) && $this->param ['create_time_start']) {
            $query->createTimeStart($this->param ['create_time_start']);
        }
        if (isset ($this->param ['create_time_end']) && $this->param ['create_time_end']) {
            $query->createTimeEnd($this->param ['create_time_end']);
        }
        if (isset ($this->param ['confirm_time_start']) && $this->param ['confirm_time_start']) {
            $query->confirmTimeStart($this->param ['confirm_time_start']);
        }
        if (isset ($this->param ['confirm_time_end']) && $this->param ['confirm_time_end']) {
            $query->confirmTimeEnd($this->param ['confirm_time_end']);
        }
        if (isset ($this->param ['delivery_date']) && $this->param ['delivery_date']) {
            $query->deliveryDate($this->param ['delivery_date']);
        }
        if (isset ($this->param ['pay_time_start']) && $this->param ['pay_time_start']) {
            $query->payTimeStart($this->param ['pay_time_start']);
        }
        if (isset ($this->param ['pay_time_end']) && $this->param ['pay_time_end']) {
            $query->payTimeEnd($this->param ['pay_time_end']);
        }

        if (isset ($this->param ['min_order_money']) && $this->param ['min_order_money']) {
            $query->where('order_money', '>=', $this->param ['min_order_money'] * 100);
        }
        if (isset ($this->param ['max_order_money']) && $this->param ['max_order_money']) {
            $query->where('order_money', '<=', $this->param ['max_order_money'] * 100);
        }
        if (isset ($this->param ['pay_type']) && $this->param ['pay_type']) {
            $query->payType($this->param ['pay_type']);
        }
        if (isset ($this->param ['pay_no']) && $this->param ['pay_no']) {
            $query->payNo($this->param ['pay_no']);
        }
        if (isset ($this->param ['shopping_time_start']) && $this->param ['shopping_time_start']) {
            $query->shoppingTimeStart($this->param ['shopping_time_start']);
        }
        if (isset ($this->param ['shopping_time_end']) && $this->param ['shopping_time_end']) {
            $query->shoppingTimeEnd($this->param ['shopping_time_end']);
        }
        if (isset ($this->param ['shopping_state']) && $this->param ['shopping_state']) {
            $query->shoppingState($this->param ['shopping_state']);
        }
        if (isset ($this->param ['jun_order_type']) && $this->param ['jun_order_type']) {
            $query->junOrderType($this->param ['jun_order_type']);
        }
        if (isset ($this->param ['caigou_invoice_time_start']) && $this->param ['caigou_invoice_time_start']) {
            $query->caigouinvoiceTimeStart($this->param ['caigou_invoice_time_start']);
        }
        if (isset ($this->param ['caigou_invoice_time_end']) && $this->param ['caigou_invoice_time_end']) {
            $query->caigouinvoiceTimeEnd($this->param ['caigou_invoice_time_end']);
        }
        if (isset ($this->param ['apply_shehe_time_start']) && $this->param ['apply_shehe_time_start']) {
            $query->applysheheTimeStart($this->param ['apply_shehe_time_start']);
        }
        if (isset ($this->param ['apply_shehe_time_end']) && $this->param ['apply_shehe_time_end']) {
            $query->applysheheTimeEnd($this->param ['apply_shehe_time_end']);
        }
        if (isset ($this->param ['apply_time_start']) && $this->param ['apply_time_start']) {
            $query->applyTimeStart($this->param ['apply_time_start']);
        }
        if (isset ($this->param ['apply_time_end']) && $this->param ['apply_time_end']) {
            $query->applyTimeEnd($this->param ['apply_time_end']);
        }
        if (isset ($this->param ['apply_shehe_time_start_2']) && $this->param ['apply_shehe_time_start_2']) {
            $query->applysheheTimeStart($this->param ['apply_shehe_time_start2']);
        }
        if (isset ($this->param ['apply_shehe_time_end_2']) && $this->param ['apply_shehe_time_end_2']) {
            $query->applysheheTimeEnd($this->param ['apply_shehe_time_end_2']);
        }
        if (isset ($this->param ['apply_time_start_2']) && $this->param ['apply_time_start_2']) {
            $query->applyTimeStart($this->param ['apply_time_start_2']);
        }
        if (isset ($this->param ['apply_time_end_2']) && $this->param ['apply_time_end_2']) {
            $query->applyTimeEnd($this->param ['apply_time_end_2']);
        }
        if (isset ($this->param ['receipt_time_start']) && $this->param ['receipt_time_start']) {
            $query->receiptTimeStart($this->param ['receipt_time_start']);
        }
        if (isset ($this->param ['receipt_time_end']) && $this->param ['receipt_time_end']) {
            $query->receiptTimeEnd($this->param ['receipt_time_end']);
        }
        if (isset ($this->param ['receipt_state']) && $this->param ['receipt_state']) {
            $query->receiptState($this->param ['receipt_state']);
        }
        if (isset ($this->param ['invoice_state']) && $this->param ['invoice_state']) {
            $query->invoiceState($this->param ['invoice_state']);
        }
        if (isset ($this->param ['caigou_state']) && $this->param ['caigou_state']) {
            $query->caigouState($this->param ['caigou_state']);
        }
        if (isset ($this->param ['purchase_state']) && $this->param ['purchase_state']) {
            $query->purchaseState($this->param ['purchase_state']);
        }
        if (isset ($this->param ['is_invoice']) && $this->param ['is_invoice']) {
            $query->isInvoice($this->param ['is_invoice']);
        }
        if (isset ($this->param ['out_order_no']) && $this->param ['out_order_no']) {
            $query->outOrderNo($this->param ['out_order_no']);
        }
        if (isset ($this->param ['shopping_id']) && $this->param ['shopping_id']) {
            $query->shoppingId($this->param ['shopping_id']);
        }

        if (isset ($this->param ['shoukuan_pay']) && $this->param ['shoukuan_pay']) {
            $query->shoukuanPay($this->param ['shoukuan_pay']);
        }
        if (isset ($this->param ['susername']) && $this->param ['susername']) {
            $query->susername($this->param ['susername']);
        }
        if (isset ($this->param ['yewuyuan']) && $this->param ['yewuyuan']) {
            $query->yewuyuan($this->param ['yewuyuan']);
        }
        if (isset ($this->param ['zhuli']) && $this->param ['zhuli']) {
            $query->zhuli($this->param ['zhuli']);
        }
        if (isset ($this->param ['neiqin']) && $this->param ['neiqin']) {
            $query->neiqin($this->param ['neiqin']);
        }
        if (isset ($this->param ['chuliren']) && $this->param ['chuliren']) {
            $query->chuliren($this->param ['chuliren']);
        }
        if (isset ($this->param ['supplier_state']) && $this->param ['supplier_state']) {
            $query->supplierState($this->param ['supplier_state']);
        }
        if (isset ($this->param ['order_type']) && $this->param ['order_type']) {
            $query->orderType($this->param ['order_type']);
        }
        if (isset ($this->param ['shouhou_type']) && $this->param ['shouhou_type']) {
            $query->shouhouType($this->param ['shouhou_type']);
        }
        /*if (isset ($this->param ['is_down']) && $this->param ['is_down']) {
            $query->orderType($this->param ['is_down']);
        }*/
        if (isset ($this->param ['is_change']) && $this->param ['is_change']) {
            $query->isChange($this->param ['is_change']);
        }
        if (isset ($this->param ['caigou_invoice_duty']) && $this->param ['caigou_invoice_duty']) {
            $query->caigouinvoiceDuty($this->param ['caigou_invoice_duty']);
        }
        if (isset ($this->param ['caigou_hetong']) && $this->param ['caigou_hetong']) {
            $query->caigouheTong($this->param ['caigou_hetong']);
        }
        if (isset ($this->param ['gys_fh_state']) && $this->param ['gys_fh_state']) {
            $query->gysfhState($this->param ['gys_fh_state']);
        }
        if (isset ($this->param ['gys_sh_state']) && $this->param ['gys_sh_state']) {
            $query->gysshState($this->param ['gys_sh_state']);
        }
        if (isset ($this->param ['ghs_pay']) && $this->param ['ghs_pay']) {
            $query->ghsPay($this->param ['ghs_pay']);
        }
        if (isset ($this->param ['api_param']) && $this->param ['api_param']) {
            $query->apiParam($this->param ['api_param']);
        }
        if (isset ($this->param ['order_type_time_start']) && $this->param ['order_type_time_start']) {
            $query->orderTypeTimeStart($this->param ['order_type_time_start']);
        }
        if (isset ($this->param ['order_type_time_end']) && $this->param ['order_type_time_end']) {
            $query->orderTypeTimeEnd($this->param ['order_type_time_end']);
        }
        if (isset ($this->param ['supplier_confirm_time_start']) && $this->param ['supplier_confirm_time_start']) {
            $query->supplierConfirmTimeStart($this->param ['supplier_confirm_time_start']);
        }
        if (isset ($this->param ['supplier_confirm_time_end']) && $this->param ['supplier_confirm_time_end']) {
            $query->supplierConfirmTimeEnd($this->param ['supplier_confirm_time_end']);
        }
        if (isset ($this->param ['push_supplier_time_start']) && $this->param ['push_supplier_time_start']) {
            $query->pushSupplierTimeStart($this->param ['push_supplier_time_start']);
        }
        if (isset ($this->param ['push_supplier_time_end']) && $this->param ['push_supplier_time_end']) {
            $query->pushSupplierTimeEnd($this->param ['push_supplier_time_end']);
        }
        if (isset ($this->param ['gys_fh_time_start']) && $this->param ['gys_fh_time_start']) {
            $query->gysFhTimeStart($this->param ['gys_fh_time_start']);
        }
        if (isset ($this->param ['gys_fh_time_end']) && $this->param ['gys_fh_time_end']) {
            $query->gysFhTimeEnd($this->param ['gys_fh_time_end']);
        }
        if (isset ($this->param ['gys_fh_time_start_2']) && $this->param ['gys_fh_time_start_2']) {
            $query->gysFhTimeStart($this->param ['gys_fh_time_start_2']);
        }
        if (isset ($this->param ['gys_fh_time_end_2']) && $this->param ['gys_fh_time_end_2']) {
            $query->gysFhTimeEnd($this->param ['gys_fh_time_end_2']);
        }
        if (isset ($this->param ['jc_fh_time_start']) && $this->param ['jc_fh_time_start']) {
            $query->jcFhTimeStart($this->param ['jc_fh_time_start']);
        }
        if (isset ($this->param ['jc_fh_time_end']) && $this->param ['jc_fh_time_end']) {
            $query->jcFhTimeEnd($this->param ['jc_fh_time_end']);
        }
        if (isset ($this->param ['ghs_pay_time_start']) && $this->param ['ghs_pay_time_start']) {
            $query->ghsPayTimeStart($this->param ['ghs_pay_time_start']);
        }
        if (isset ($this->param ['ghs_pay_time_end']) && $this->param ['ghs_pay_time_end']) {
            $query->ghsPayTimeEnd($this->param ['ghs_pay_time_end']);
        }
        if (isset ($this->param ['gys_sh_time_start']) && $this->param ['gys_sh_time_start']) {
            $query->gysShTimeStart($this->param ['gys_sh_time_start']);
        }
        if (isset ($this->param ['gys_sh_time_end']) && $this->param ['gys_sh_time_end']) {
            $query->gysShTimeEnd($this->param ['gys_sh_time_end']);
        }
        if (isset ($this->param ['gys_sh_time_start_2']) && $this->param ['gys_sh_time_start_2']) {
            $query->gysShTimeStart($this->param ['gys_sh_time_start_2']);
        }
        if (isset ($this->param ['gys_sh_time_end_2']) && $this->param ['gys_sh_time_end_2']) {
            $query->gysShTimeEnd($this->param ['gys_sh_time_end_2']);
        }
        if (isset ($this->param ['invoice_time_start']) && $this->param ['invoice_time_start']) {
            $query->invoiceTimeStart($this->param ['invoice_time_start']);
        }
        if (isset ($this->param ['invoice_time_end']) && $this->param ['invoice_time_end']) {
            $query->invoiceTimeEnd($this->param ['invoice_time_end']);
        }
        if (isset ($this->param ['invoice_time_start_2']) && $this->param ['invoice_time_start_2']) {
            $query->invoiceTimeStart($this->param ['invoice_time_start_2']);
        }
        if (isset ($this->param ['invoice_time_end_2']) && $this->param ['invoice_time_end_2']) {
            $query->invoiceTimeEnd($this->param ['invoice_time_end_2']);
        }
        if (isset ($this->param ['jc_sh_time_start']) && $this->param ['jc_sh_time_start']) {
            $query->jcShTimeStart($this->param ['jc_sh_time_start']);
        }
        if (isset ($this->param ['jc_sh_time_end']) && $this->param ['jc_sh_time_end']) {
            $query->jcShTimeEnd($this->param ['jc_sh_time_end']);
        }
        if (isset ($this->param ['ruku_time_start']) && $this->param ['ruku_time_start']) {
            $query->rukuTimeStart($this->param ['ruku_time_start']);
        }
        if (isset ($this->param ['ruku_time_end']) && $this->param ['ruku_time_end']) {
            $query->rukuTimeEnd($this->param ['ruku_time_end']);
        }
        if (isset ($this->param ['ruku_time_start_2']) && $this->param ['ruku_time_start_2']) {
            $query->rukuTimeStart($this->param ['ruku_time_start_2']);
        }
        if (isset ($this->param ['ruku_time_end_2']) && $this->param ['ruku_time_end_2']) {
            $query->rukuTimeEnd($this->param ['ruku_time_end_2']);
        }
        if (isset ($this->param ['is_reset']) && $this->param ['is_reset'] == 'yes') {
            $query->isReset();
        }
        if (isset ($this->param ['is_feizhi']) && $this->param ['is_feizhi'] == 'yes') {
            $query->where('order_state', -1);
        }
        if (isset ($this->param ['truename']) && $this->param ['truename']) {
            $query->where('api_param', 'like', '%' . $this->param ['truename'] . '%');
        }
        if (isset ($this->param ['phone']) && $this->param ['phone']) {
            //$query->where('api_param', 'like', '%' . $this->param ['phone'] . '%');
            $query->where('phone', 'like', '%' . $this->param ['phone'] . '%');
        }
        if (isset ($this->param ['j_brand']) && $this->param ['j_brand']) {
            $query->where('api_param', 'like', '%' . $this->param ['j_brand'] . '%');
        }
        if (isset ($this->param ['j_province']) && $this->param ['j_province']) {
            $query->where('api_param', 'like', '%' . $this->param ['j_province'] . '%');
        }
        if (isset ($this->param ['j_address']) && $this->param ['j_address']) {
            $query->jAddress($this->param ['j_address']);
            // $query->where ( 'api_param', 'like', '%' . urldecode ($this->param ['j_address']) . '%' );
        }
        if (isset($this->param['pname']) && $this->param['pname']) {
            $query->where('api_param', 'exp', 'like "%' . $this->param['pname'] . '%" OR api_param like "%\"' . trim($this->param['pname_id']) . '\"%"');
        }
        if (isset($this->param['cname']) && $this->param['cname']) {
            $query->where('api_param', 'exp', 'like "%' . $this->param['cname'] . '%" OR api_param like "%\"' . trim($this->param['cname_id']) . '\"%"');
        }
        if (isset($this->param['jun_order_state']) && $this->param['jun_order_state'] != '') {
            $query->where('jun_order_state', $this->param['jun_order_state']);
        }
        if (isset($this->param['returncode']) && $this->param['returncode'] != '') {
            $query->returncode($this->param['returncode']);
        }
        if (isset($this->param['apply_state']) && $this->param['apply_state']) {
            if ($this->param['apply_state'] == -1) {
                $query->where('apply_state', '>', 1);
            } else {
                $query->where('apply_state', $this->param['apply_state']);
            }
            /*$cartdb = new Cartdb();
            if ($this->param['apply_state'] == -1) {
                $query->whereIn('order_no', $cartdb->fetchSql(true)->where('apply_state', '>', 1)->column('order_no'));
            } else {
                $query->whereIn('order_no', $cartdb->fetchSql(true)->where('apply_state', $this->param['apply_state'])->column('order_no'));
            }*/

        }
        if (isset($this->param['is_down']) && $this->param['is_down']) {
            $query->where('is_down', $this->param['is_down']);
        }
        if (isset($this->param['is_change']) && $this->param['is_change']) {
            $query->where('is_change', $this->param['is_change']);
        }
        if (isset($this->param['invoice_title']) && $this->param['invoice_title']) {
            //$invoicedb = new Invoicedb();
            //$get_invoice = Db::name('invoice')->fetchSql(true)->whereLike('invoice_title', '%' . $this->param['invoice_title'] . '%')->column('order_nos');
            //$query->whereIn('order_no', $get_invoice);
            $get_invoice = Db::name('invoice')->fetchSql(true)->where('invoice_title', $this->param['invoice_title'])->column('order_nos');
            $query->where('order_no', 'exp', 'in (' . $get_invoice . ')');
        }
        if (isset($this->param['invoice_duty']) && $this->param['invoice_duty']) {
            $invoicedb = new Invoicedb();
            $get_invoice = $invoicedb->whereLike('invoice_duty', '%' . $this->param['invoice_duty'] . '%')->column('order_nos');
            $query->whereIn('order_no', $get_invoice);
        }
        if (isset($this->param['invoice_beizhu']) && $this->param['invoice_beizhu']) {
            $invoicedb = new Invoicedb();
            $get_invoice = $invoicedb->whereLike('invoice_beizhu', '%' . $this->param['invoice_beizhu'] . '%')->column('order_nos');
            $query->whereIn('order_no', $get_invoice);
        }
        if (isset ($this->param ['reason']) && $this->param ['reason']) {
            $query->reason($this->param ['reason']);
        }

        if (isset($this->param['out_order_no_arr']) && $this->param['out_order_no_arr']) {
            $query->whereIn('out_order_no', $this->param['out_order_no_arr']);
        }

        if (isset($this->param['out_order_no_arr']) && $this->param['out_order_no_arr']) {
            $query->whereIn('out_order_no', $this->param['out_order_no_arr']);
        }

        if (isset($this->param['wuliu']) && $this->param['wuliu']) {
            $query->where('wuliu', $this->param['wuliu']);
        }
        if (isset($this->param['dingyue']) && $this->param['dingyue']) {
            $query->where('dingyue', $this->param['dingyue']);
        }

        // 申请电子票查询
        if (isset($this->param['invoice_api_bianhao']) && $this->param['invoice_api_bianhao']) {
            $get_invoice = Db::name('invoice')->fetchSql(true)->whereNotNull('api_bianhao')->column('order_nos');
            if ($this->param['invoice_api_bianhao'] == 1) {
                $query->where('order_no', 'exp', 'in (' . $get_invoice . ')');
            } else {
                $query->where('order_no', 'exp', 'not in (' . $get_invoice . ')');
            }
        }
        // 申请电子票结束

        // 精准搜索
        $order_search = Db::name('member')->where('member_id', MEMBER_ID)->field('order_search,view_pre_order')->find();
        if ($order_search['order_search'] == 2) {
            $supplier_id_arr = Db::name('member')
                ->where('', 'exp', 'FIND_IN_SET(' . MEMBER_ID . ',be_staff_id)')
                ->column('member_id');
            if (count($supplier_id_arr)) {
                if ($order_search['view_pre_order'] == 1) {
                    $supplier_id_arr_new = $supplier_id_arr;
                    array_push($supplier_id_arr_new, 0);
                    $query->where(function ($query) use ($supplier_id_arr, $supplier_id_arr_new) {
                        $query->whereIn('supplier_id', $supplier_id_arr_new, 'or');
                        $query->whereIn('supplier_er', $supplier_id_arr, 'or');
                    });
                } else {
                    $query->whereIn('supplier_id|supplier_er', $supplier_id_arr);
                }
            } else {
                if ($order_search['view_pre_order'] == 1) {
                    $query->where('supplier_id', 0);
                } else {
                    $query->where('order_id', 0);
                }
            }
        } else {
            if ($order_search['view_pre_order'] == 2) {
                $query->where('supplier_id', '>', 0);
            }
        }

    }

    public function scopeReason($query, $reason)
    {
        $query->where('reason', 'IN', $reason);
    }

    public function scopeJAddress($query, $j_address)
    {
        $shoppingdb = new Shoppingdb ();
        $get_shopping = $shoppingdb->useGlobalScope(false)->where('member_id', 193)->where('street', 'like', '%' . $j_address . '%')->select();
        if ($get_shopping) {
            $get_shopping = $get_shopping->toArray();
            $shopping_id = array();
            foreach ($get_shopping as $k => $v) {
                array_push($shopping_id, $v ['shopping_id']);
            }
            $query->where('shopping_id', 'IN', $shopping_id);
        } else {
            $query->where('api_param', 'like', '%' . $j_address . '%');
        }
    }

    // 精准搜索
    public function scopeJAddressJz($query, $j_address)
    {
        $shoppingdb = new Shoppingdb ();
        $get_shopping = $shoppingdb->useGlobalScope(false)->where('member_id', 193)->where('street', $j_address)->select();
        if ($get_shopping) {
            $get_shopping = $get_shopping->toArray();
            $shopping_id = array();
            foreach ($get_shopping as $k => $v) {
                array_push($shopping_id, $v ['shopping_id']);
            }
            $query->where('shopping_id', 'IN', $shopping_id);
        }
    }

    public function scopeIsReset($query)
    {
        $query->where('is_reset', 1);
    }

    public function scopeJcShTimeStart($query, $time)
    {
        $query->whereTime('jc_sh_time', '>=', $time);
    }

    public function scopeIsChange($query, $is_change)
    {

        if (isset($is_change) && $is_change != '') {
            $query->where('is_change', $is_change);
        }
    }

    public function scopeIsDown($query, $time)
    {
        $query->whereTime('is_down', '>=', $time);
    }

    public function scopeJcShTimeEnd($query, $time)
    {
        $query->whereTime('jc_sh_time', '<', $time);
    }

    public function scopeCaigouInvoiceTimeStart($query, $time)
    {
        $query->whereTime('caigou_invoice_time', '>=', $time);
    }

    public function scopeCaigouInvoiceTimeEnd($query, $time)
    {
        $query->whereTime('caigou_invoice_time', '<', $time);
    }

    public function scopeGhsPayTimeStart($query, $time)
    {
        $query->whereTime('ghs_pay_time', '>=', $time);
    }

    public function scopeGhsPayTimeEnd($query, $time)
    {
        $time = strtotime($time);
        $date = strtotime('+1 day', $time);
        $query->whereTime('ghs_pay_time', '<', $date);
    }

    public function scopeGysShTimeStart($query, $time)
    {
        $query->whereTime('gys_sh_time', '>=', $time);
    }

    public function scopeGysShTimeEnd($query, $time)
    {
        $query->whereTime('gys_sh_time', '<', $time);
    }

    public function scopeRukuTimeStart($query, $time)
    {
        $query->whereTime('ruku_time', '>=', $time);
    }

    public function scopeRukuTimeEnd($query, $ruku_time)
    {
        $time = strtotime($ruku_time);
        $date = strtotime('+1 day', $time);
        $query->whereTime('ruku_time', '<', $date);
    }

    public function scopeOrderTypeTimeStart($query, $time)
    {
        $query->whereTime('order_type_time', '>=', $time);
    }

    public function scopeOrderTypeTimeEnd($query, $time)
    {
        $query->whereTime('order_type_time', '<', $time);
    }

    public function scopeJcFhTimeStart($query, $time)
    {
        $query->whereTime('jc_fh_time', '>=', $time);
    }

    public function scopeJcFhTimeEnd($query, $time)
    {
        $query->whereTime('jc_fh_time', '<', $time);
    }

    public function scopeGysFhTimeStart($query, $time)
    {
        $query->whereTime('gys_fh_time', '>=', $time);
    }

    public function scopeGysFhTimeEnd($query, $time)
    {

        $query->whereTime('gys_fh_time', '<', $time);
    }

    public function scopePushSupplierTimeStart($query, $time)
    {
        $query->whereTime('push_supplier_time', '>=', $time);
    }

    public function scopePushSupplierTimeEnd($query, $time)
    {
        $time = strtotime($time);
        $date = strtotime('+1 day', $time);
        $query->whereTime('push_supplier_time', '<', $date);
    }

    public function scopeSupplierConfirmTimeStart($query, $time)
    {
        $query->whereTime('supplier_confirm_time', '>=', $time);
    }

    public function scopeSupplierConfirmTimeEnd($query, $time)
    {
        $time = strtotime($time);
        $date = strtotime('+1 day', $time);
        $query->whereTime('supplier_confirm_time', '<', $date);
    }

    public function scopeApiParam($query, $api_param)
    {
        $query->where('api_param', 'like', '%' . $api_param . '%');
    }

    public function scopeRemark($query, $remark)
    {
        $query->whereLike('remark', '%' . $remark . '%');
    }

    public function scopeRemarkNot($query, $remark)
    {
        $query->whereNotNull('remark');
        $query->whereNotLike('remark', '%' . $remark . '%');
    }

    public function scopeMemberRemark($query, $member_remark)
    {
        $query->whereLike('member_remark', '%' . $member_remark . '%');
    }

    public function scopeMemberRemarkNot($query, $member_remark)
    {
        $query->whereNotNull('member_remark');
        $query->whereNotLike('member_remark', '%' . $member_remark . '%');
    }

    public function scopeSupplerBz($query, $suppler_bz)
    {
        $query->whereLike('suppler_bz', '%' . $suppler_bz . '%');
    }

    public function scopeSupplerBzNot($query, $suppler_bz)
    {
        $query->whereNotNull('suppler_bz');
        $query->whereNotLike('suppler_bz', '%' . $suppler_bz . '%');
    }

    public function scopeSupplerBz2($query, $suppler_bz2)
    {
        $query->where('suppler_bz2', 'like', '%' . $suppler_bz2 . '%');
    }

    public function scopeSupplerBz3($query, $suppler_bz3)
    {
        $query->where('suppler_bz3', 'like', '%' . $suppler_bz3 . '%');
    }

    public function scopeOrderType($query, $order_type)
    {
        if ($order_type == 11) {
            $query->whereNotIn('order_type', [5, 7]);
        } else {
            $query->where('order_type', $order_type);
        }

    }

    public function scopeShouhouType($query, $shouhou_type)
    {
        if ($shouhou_type == 100) {
            $query->where('shouhou_type', '>', 1);
        } else {
            $query->where('shouhou_type', $shouhou_type);
        }

    }

    public function scopeCaiGouInvoiceDuty($query, $caigou_invoice_duty)
    {
        if ($caigou_invoice_duty == 1) {
            $query->where('caigou_invoice_duty', null);
        } elseif ($caigou_invoice_duty == 2) {
            $query->where('caigou_invoice_duty', 'not null');
        }
    }

    public function scopeReturnCode($query, $returncode)
    {
        if ($returncode == 1) {
            $query->where('returncode', null);
        } elseif ($returncode == 2) {
            $query->where('returncode', '成功');
        } elseif ($returncode == 3) {
            $query->where('returncode', '线下开具成功');
        } elseif ($returncode == 4) {
            $query->where('returncode', '红冲成功');
        } elseif ($returncode == 5) {
            $query->whereNotLike('returncode', '%成功%');
        }
    }

    public function scopeCaiGouHeTong($query, $caigou_hetong)
    {
        if ($caigou_hetong == 1) {
            $query->where('caigou_hetong', null);
        } elseif ($caigou_hetong == 2) {
            $query->where('caigou_hetong', 'not null');
        }
    }

    public function scopeGysFhState($query, $gys_fh_state)
    {
        $query->where('gys_fh_state', $gys_fh_state);
    }

    public function scopeGysShState($query, $gys_sh_state)
    {
        $query->where('gys_sh_state', $gys_sh_state);
    }

    public function scopeGhsPay($query, $ghs_pay)
    {
        $query->where('ghs_pay', $ghs_pay);
    }

    public function scopeSupplierState($query, $supplier_state)
    {
        $query->where('supplier_state', $supplier_state);
    }

    public function scopeShoukuanPay($query, $shoukuan_pay)
    {
        $query->where('shoukuan_pay', $shoukuan_pay);
    }

    public function scopeSupplierId($query, $supplier_id)
    {
        $query->where('supplier_id', $supplier_id);
    }

    // 分组查询
    public function ajaxGroup()
    {
        $data = $this->field($this->param ['field'])->group($this->param ['field'])->select()->toArray();
        return $data;
    }

    public function ajaxOrderNo()
    {
        $data = $this->useGlobalScope(false)->where('out_order_no', $this->param ['out_order_no'])->find();
        if ($data) {
            $data = $data->toArray();
        } else {
            $data = array();
        }
        return $data;
    }

    public function ajaxFind()
    {
        if (isset ($this->param ['order_no'])) {
            $data = $this->useGlobalScope(false)->master()->where('order_no', $this->param ['order_no'])->find();
        } else {
            $data = $this->useGlobalScope(false)->master()->find();
        }
        if ($data) {
            $data = $data->toArray();
            $data = $this->handleData($data);
        } else {
            $data = 'fail';
        }
        return $data;
    }

    // 只获取字段
    public function ajaxField()
    {
        $data = $this->useGlobalScope(false)->master()->where('order_no', $this->param ['order_no'])->field($this->param ['field'])->find();
        if ($data) {
            $data = $data->toArray();
        } else {
            $data [$this->param ['field']] = '';
        }
        return $data;
    }

    public function saveStateBack()
    {
        $data = $this->find()->toArray();
        $states = $data ['order_state'] - 1;
        $this->save([
            'order_state' => $states
        ], [
            'order_id' => $data ['order_id']
        ]);
        return 'success';
    }

    public function ajaxSaveInvoiceState()
    {
        $this->where('order_no', 'IN', $this->param ['order_no'])->update(array(
            'invoice_state' => 2
        ));
        Cache::clear('order');
        $cartdb = new Cartdb ();
        $cartdb->param ['order_no'] = $this->param ['order_no'];
        $get_state = $cartdb->saveInvoiceState();
        return 'success';
    }

    public function ajaxSaveOne()
    {
        if (isset ($this->param ['set_key'])) {
            $get_data = $this->useGlobalScope(false)->where($this->param ['set_key'] . '="' . $this->param ['order_id'] . '"')->find()->toArray();
            $this->param ['order_id'] = $get_data ['order_id'];
        }
        $this->save($this->param ['data'], [
            'order_id' => $this->param ['order_id']
        ]);
        Cache::clear('order');
        return 'success';
    }

    public function clearCache()
    {
        Cache::clear('order');
        return 'success';
    }

    public function ajaxState($order_no)
    {
        $data = $this->useGlobalScope(false)->master()->where('order_no', $order_no)->find()->toArray();
        return $data ['order_state'];
    }

    public function ajaxStateCount()
    {
        $return ['all_order'] = $this->useGlobalScope(false)->where('member_id', $this->param['member_id'])->count();
        $return ['wait_pay'] = $this->useGlobalScope(false)->whereIn('pay_state', [1, 3])->where('member_id', $this->param['member_id'])->count();
        $return ['wait_confirm'] = $this->useGlobalScope(false)->where('pay_state', 1)->where('member_id', $this->param['member_id'])->count();
        $return ['wait_post'] = $this->useGlobalScope(false)->where('pay_state', 2)->where('member_id', $this->param['member_id'])->count();
        $return ['wait_receipt'] = $this->useGlobalScope(false)->where('order_state', 3)->where('member_id', $this->param['member_id'])->count();
        $return ['order_over'] = $this->useGlobalScope(false)->where('order_state', 5)->where('member_id', $this->param['member_id'])->count();
        $return ['order_cancel'] = $this->useGlobalScope(false)->where('order_state', -1)->where('member_id', $this->param['member_id'])->count();
        $return ['wait_judge'] = $this->useGlobalScope(false)->where('order_state', 4)->whereIn('judge_state', [1, 3])->where('member_id', $this->param['member_id'])->count();
        return $return;
    }

    public function ajaxPriceCount()
    {
        $count = $this->cache($this->cache_name('order_order_money'), 86400, 'order')->sum('order_money');
        return $count;
    }

    // 查询
    public function ajaxSelect()
    {
        $return ['data'] = $this->ajaxList();
        $return ['count'] = $this->ajaxCount();
        return $return;
    }

    public function ajaxList()
    {
        /*if(isset($this->param['zhuijia']) && $this->param['zhuijia'] == 'yes'){
            $data = $this->fetchSql(true)->order($this->param ['_o'], $this->param ['_b'])->page($this->param ['page'], $this->param ['limit'])->select();
            halt($data);
        }*/
        $data = $this->order($this->param ['_o'], $this->param ['_b'])->page($this->param ['page'], $this->param ['limit'])->select();

        if (!$data->isEmpty()) {
            $data = $data->toArray();
            if (!isset ($this->param ['no_need_order_handle'])) {
                foreach ($data as $key => $val) {
                    $data [$key] = $this->handleData($val);
                }
                if (isset ($this->param ['is_reset']) && $this->param ['is_reset'] == 'yes') {
                    $new_data = array();
                    foreach ($data as $key => $val) {
                        $tx = json_decode($val ['reset_suppler'], true);
                        for ($x = 0; $x < (count($tx) - 1); $x++) {
                            $data [$key] ['supplier_name'] = $tx [$x] ['supplier_name'];
                            $data [$key] ['zhipai_date'] = $tx [$x] ['zhipai_date'];
                            array_push($new_data, $data [$key]);
                            $data [$key] ['order_money'] = -$val ['order_money'];
                            array_push($new_data, $data [$key]);
                        }
                    }
                    $data = $new_data;
                }
            }
        }
        return $data;
    }

    // 新增订单
    public function addOrder()
    {
        //Db::startTrans();
        if ($this->param ['order_money'] == 0) {
            Db::rollback();
            return '订单金额错误';
        }
        unset ($this->data ['order_id']);
        $this->param['phone'] = Db::name('shopping')->where('shopping_id', $this->param['shopping_id'])->value('phone');
        $this->isUpdate(false)->allowField(true)->save($this->param);
        if (isset ($this->param ['select_goods_info'])) {
            // 后台
            if (count($this->param ['select_goods_info']) == 0) {
                Db::rollback();
                return '未选择商品';
            }
            // 新增购物车
            $cartdb = new Cartdb ();
            $cartdb->param ['cart_info'] = $this->param ['select_goods_info'];
            $cartdb->param ['order_no'] = $this->data ['order_no'];
            if (SITE_ID == 109 || SITE_ID == 115 || SITE_ID == 117) {
                foreach ($cartdb->param ['cart_info'] as $key => $val) {
                    $cartdb->param ['cart_info'] [$key] ['order_no'] = $this->data ['order_no'];
                }
                $cart_return = $cartdb->addCartNoChkgoods();
            } else {
                $cart_return = $cartdb->addCart();
            }
            if ($cart_return != 'success') {
                Db::rollback();
                return $cart_return;
            }
        } else {
            // 前台
            $cartdb = new Cartdb ();
            $cartdb->aloneSave([
                'order_no' => $this->data ['order_no']
            ], $this->param ['cart_ids']);
        }

        // 发票管理
        if ($this->param ['is_invoice'] == 2) {
            $invoicedb = new Invoicedb ();
            $invoicedb->param = $this->param;
            $invoicedb->param ['order_nos'] = $this->data ['order_no'];
            $invoicedb->param ['cart_ids'] = $cartdb->inset_ids;
            $invoicedb->ajaxSave();
        }
        // 流转日志
        $order_logdb = new OrderLogdb ();
        $order_logdb->param = array(
            'type_index' => 1,
            'order_no' => $this->data ['order_no'],
            'member_id' => $this->data ['order_member_id'],
            'view_text' => '创建订单',
            'post_json' => $this->param
        );
        $order_logdb->saveData();
        //Db::commit();
        $this->last_order_no = $this->data ['order_no'];
        Cache::clear('order');
        return 'success';
    }

    // 修改订单信息
    public function editOrder()
    {
        switch ($this->param ['event']) {
            case 'shenqing_pass' :
                $save_data ['confirm_shoukuan_pay'] = $this->param ['confirm_shoukuan_pay'];
                if ($this->param ['confirm_shoukuan_pay'] == 1) {
                    $view_text = '通过收款申请';
                    $save_data ['pay_state'] = $this->param['shenqing_type'];
                    $save_data ['pay_time'] = time();
                    // 设置优士收款状态
                    $invoicedb = new Invoicedb ();
                    $invoicedb->useGlobalScope(false)->where('order_nos', $this->param ['order_no'])->update([
                        'order_shoukuan' => $this->param['shenqing_type']
                    ]);
                } else {
                    $view_text = '驳回收款申请';
                }
                $this->save($save_data, [
                    'order_no' => $this->param ['order_no']
                ]);
                $order_logdb = new OrderLogdb ();
                $order_logdb->param = array(
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => $view_text,
                    'post_json' => $this->param
                );
                $order_logdb->saveData();
                break;
            case 'removeInvoice' :
                // 用户删除采购发票合同
                $get_order = $this->where('order_no', $this->param ['order_no'])->find()->toArray();
                $temp ['caigou_invoice_duty'] = explode(',', $get_order ['caigou_invoice_duty']);
                $temp ['caigou_invoice_pz'] = explode(',', $get_order ['caigou_invoice_pz']);
                $index = $this->param ['index'];
                $new_duty = array();
                $new_pz = array();
                for ($i = 0; $i < count($temp ['caigou_invoice_duty']); $i++) {
                    if ($i != $index) {
                        if (isset($temp ['caigou_invoice_duty'] [$i])) {
                            array_push($new_duty, $temp ['caigou_invoice_duty'] [$i]);
                        }
                        if (isset($temp ['caigou_invoice_pz'] [$i])) {
                            array_push($new_pz, $temp ['caigou_invoice_pz'] [$i]);
                        }
                    }
                }
                $save_data ['caigou_invoice_duty'] = implode(',', $new_duty);
                $save_data ['caigou_invoice_pz'] = implode(',', $new_pz);
                $this->save($save_data, [
                    'order_no' => $this->param ['order_no']
                ]);
                break;
            case 'songhuodan_xieyi' :
                $order_logdb = new OrderLogdb ();
                $order_logdb->param = array(
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => '上传送货单',
                    'post_json' => $this->param
                );
                $order_logdb->saveData();
                $order_no = $this->param ['order_no'];
                unset ($this->param ['order_no']);
                $this->save($this->param, [
                    'order_no' => $order_no
                ]);
                break;
            case 'xiaoshou_xieyi' :
                $order_logdb = new OrderLogdb ();
                $order_logdb->param = array(
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => '上传销售合同',
                    'post_json' => $this->param
                );
                $order_logdb->saveData();
                $order_no = $this->param ['order_no'];
                unset ($this->param ['order_no']);
                $this->save($this->param, [
                    'order_no' => $order_no
                ]);
                break;
            case 'edit_fahuo_gys' :
                $get_order = Orderdb::useGlobalScope(false)->where('order_no', $this->param['order_no'])
                    ->whereTime('create_time', '<', time() - 1.5 * 60 * 60)
                    ->find();
                if (!$get_order) {
                    return '距离订单创建时间过短,请距离订单创建时间1.5个小时后发货';
                }
                $dingyue = 4;
                $dingyue_txt = '';
                if (in_array($get_order['supplier_id'], [432, 450, 451])) {
                    $this->param['wuliu'] = '自配物流';
                }
                if (strpos($get_order['out_order_no'], 'X') !== false) {
                    $this->param['wuliu'] = '自配物流';
                }
                // 延迟推送
                if (isset($this->param['yanchi']) && $this->param['yanchi'] == 'yes') {
                    $this->param['wuliu'] = '自配物流';
                }

                $result = 'success';

                if ($this->param['wuliu'] != '自配物流') {
                    $dingyue = 2;
                    // 非自配物流 开始订阅
                    $buildtpl = new Buildtpl();
                    $dingyue_txt = $buildtpl->kuaidi100($this->param ['wuliu'], trim($this->param ['wuliudanhao']), $get_order['order_id'], $get_order['api_param'], $this->param['sjrsjh']);
                    if (strpos($dingyue_txt, '提交成功') !== false || $dingyue_txt == 'POLL:重复订阅') {
                        $result = 'success';
                    } else {
                        $result = $dingyue_txt;
                    }
                }
                if ($result != 'success') {
                    return '订阅失败：' . $dingyue_txt;
                }

                $json_str = file_get_contents('./static/js/jqcity/allcity.js');
                $json_str = str_replace('callback(', '', $json_str);
                $json_str = str_replace(');', '', $json_str);
                $json_arr = json_decode($json_str, true);
                $province_name = '';
                $city_name = '';
                for ($i = 0; $i < count($json_arr['province']); $i++) {
                    if ($this->param['province2'] == $json_arr['province'][$i]['id']) {
                        $province_name = $json_arr['province'][$i]['name'];
                        break;
                    }
                }
                for ($i = 0; $i < count($json_arr['city'][$this->param['province2']]); $i++) {
                    if ($this->param['city2'] == $json_arr['city'][$this->param['province2']][$i]['id']) {
                        $city_name = $json_arr['city'][$this->param['province2']][$i]['name'];
                        break;
                    }
                }
                $this->save([
                    'gys_fh_state' => 1,
                    'wuliu' => $this->param ['wuliu'],
                    'wuliudanhao' => $this->param ['wuliudanhao'],
                    'delivery_date' => $this->param ['delivery_date'],
                    'post_date' => $this->param['post_date'],
                    'gys_province' => $province_name,
                    'gys_city' => $city_name,
                    'sjrsjh' => $this->param['sjrsjh'],
                    'gys_fh_time' => time(),
                    'dingyue' => $dingyue,
                    'dingyue_txt' => $dingyue_txt,
                    'yanchi' => $this->param['yanchi'] ?? 'no',
                    'yanchi_time' => time()
                ], [
                    'order_no' => $this->param ['order_no']
                ]);
                // 日志
                $order_logdb = new OrderLogdb ();
                $order_logdb->param = array(
                    'type_index' => 4,
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => '库房已发货，延迟推送',
                    'post_json' => $this->param
                );
                $order_logdb->saveData();
                break;
            case 'edit_fahuo_gys_edit' :
                $get_order = Orderdb::useGlobalScope(false)->where('order_no', $this->param['order_no'])
                    ->whereTime('create_time', '<', time() - 1.5 * 60 * 60)
                    ->find();
                if (!$get_order) {
                    return '距离订单创建时间过短,请距离订单创建时间1.5个小时后发货';
                }
                $dingyue = 4;
                $dingyue_txt = '';
                if (in_array($get_order['supplier_id'], [432, 450, 451])) {
                    $this->param['wuliu_edit'] = '自配物流';
                }
                if (strpos($get_order['out_order_no'], 'X') !== false) {
                    $this->param['wuliu_edit'] = '自配物流';
                }

                // 延迟推送
                if (isset($this->param['yanchi_edit']) && $this->param['yanchi_edit'] == 'yes') {
                    $this->param['wuliu'] = '自配物流';
                }

                $result = 'success';

                if ($this->param['wuliu_edit'] != '自配物流') {
                    $dingyue = 2;
                    // 非自配物流 开始订阅
                    $buildtpl = new Buildtpl();
                    $dingyue_txt = $buildtpl->kuaidi100($this->param ['wuliu_edit'], trim($this->param ['wuliudanhao_edit']), $get_order['order_id'], $get_order['api_param'], $this->param['sjrsjh_edit']);
                    if (strpos($dingyue_txt, '提交成功') !== false || $dingyue_txt == 'POLL:重复订阅') {
                        $result = 'success';
                    } else {
                        $result = $dingyue_txt;
                    }
                }
                if ($result != 'success') {
                    return '订阅失败：' . $dingyue_txt;
                }
                $json_str = file_get_contents('./static/js/jqcity/allcity.js');
                $json_str = str_replace('callback(', '', $json_str);
                $json_str = str_replace(');', '', $json_str);
                $json_arr = json_decode($json_str, true);
                $province_name = '';
                $city_name = '';
                for ($i = 0; $i < count($json_arr['province']); $i++) {
                    if ($this->param['province2_edit'] == $json_arr['province'][$i]['id']) {
                        $province_name = $json_arr['province'][$i]['name'];
                        break;
                    }
                }
                for ($i = 0; $i < count($json_arr['city'][$this->param['province2_edit']]); $i++) {
                    if ($this->param['city2_edit'] == $json_arr['city'][$this->param['province2_edit']][$i]['id']) {
                        $city_name = $json_arr['city'][$this->param['province2_edit']][$i]['name'];
                        break;
                    }
                }


                $this->save([
                    'wuliu' => $this->param ['wuliu_edit'],
                    'wuliudanhao' => $this->param ['wuliudanhao_edit'],
                    'sjrsjh' => $this->param['sjrsjh_edit'],
                    'dingyue' => $dingyue,
                    'dingyue_txt' => $dingyue_txt,
                    'yanchi' => $this->param['yanchi_edit'] ?? '',
                    'delivery_date' => $this->param ['delivery_date_edit'],
                    'post_date' => $this->param['post_date_edit'],
                    'gys_province' => $province_name,
                    'gys_city' => $city_name,
                ], [
                    'order_no' => $this->param ['order_no']
                ]);
                // 日志
                $order_logdb = new OrderLogdb ();
                $order_logdb->param = array(
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => '修改快递信息,延迟推送：' . $this->param['yanchi_edit'] ?? '',
                    'post_json' => $this->param
                );
                $order_logdb->saveData();
                // 重新独立订阅 不能用 需要判断的有点多 直接进定时器
                /*$buildtpl = new Buildtpl();
                $buildtpl->kuaidiOne($this->param ['order_id']);*/
                break;
            case 'edit_shouhuo_gys' :
                $get_order = Orderdb::useGlobalScope(false)
                    ->where('order_no', $this->param['order_no'])
                    ->whereTime('gys_fh_time', '<', time() - 24 * 60 * 60)
                    ->find();
                if (!$get_order) {
                    return '距离订单发货时间过短,请距离订单发货时间24个小时后收货';
                }
                //Db::startTrans();
                $log_text = '订单收货';
                $type_index = 10;
                // 设置收货物流
                $expressdb = new Expressdb ();
                $get_express = $expressdb->where('order_no', $this->param ['order_no'])->find();
                $expressdb->param ['express_id'] = $get_express['express_id'];
                //   $expressdb->param ['receipt_time'] = time();
                //   $expressdb->param ['receipt_state'] = 2;
                if (isset ($this->param ['post_data'] ['member_id']) && $this->param ['post_data'] ['member_id']) {
                    $receipt_member_id = $this->param ['post_data'] ['member_id'];
                } else {
                    $receipt_member_id = MEMBER_ID;
                }
                $expressdb->param ['receipt_member_id'] = $receipt_member_id;
                $expressdb->ajaxSave();
                $get_express_info = $expressdb->ajaxFind();
                // 设置购物车产品收货状态
                $cartdb = new Cartdb ();
                $cartdb->param ['order_no'] = $this->param ['order_no'];
                $cartdb->param ['field'] = 'receipt_state';
                $cartdb->param ['value'] = 1;
                $cartdb->param ['cart_ids'] = explode(',', $get_express_info ['cart_ids']);
                $cartdb->param ['is_exchange'] = $get_express_info ['is_exchange'];
                $cartdb->editCartState();

                // 日志
                $order_logdb = new OrderLogdb ();
                // 日志
                $order_logdb->param = array(
                    'type_index' => $type_index,
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => $log_text,
                    'post_json' => ''
                );
                $order_logdb->saveData();
                // 处理供应商收货
                $this->save([
                    'gys_sh_state' => 1,
                    'gys_sh_time' => time(),
                    'qianshouyuan' => $this->param['post_data']['qianshouyuan']
                    //     'order_state' => 4,
                    //     'receipt_state' => 2,
                    //     'receipt_time' => time()
                ], [
                    'order_no' => $this->param ['order_no']
                ]);
                // 日志
                $order_logdb = new OrderLogdb ();
                $order_logdb->param = array(
                    'type_index' => 5,
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => '库房已收货',
                    'post_json' => $this->param
                );
                $order_logdb->saveData();
                $result = array('isSuccess' => true, 'returnMsg' => 'success');
                $result1 = array('isSuccess' => true, 'returnMsg' => 'success');
                // 军采推送消息
                /* if (SITE_ID == 117) {
                     $jun_api = new Api ();
                     $get_out_order = $this->useGlobalScope(false)->where('order_no', $this->param ['order_no'])->find()->toArray();
                     $get_express_info = Expressdb::where('order_no', $this->param ['order_no'])->find();
                     if ($get_express_info) {
                         $get_express_info = $get_express_info->toArray();
                         $post_json ['tradeNo'] = $get_out_order ['out_order_no'];
                         $post_json ['packageNo'] = $get_express_info ['express_id'];
                         $result = $jun_api->ajaxPackageOverNotice($post_json);
                         $result1 = $jun_api->sendOrderLogistics($get_out_order['out_order_no']);
                     }
                 }
                 */
                return '保存成功';
                //   return '推送信息:投妥状态:' . $result['returnMsg'] . ';物流状态:' . $result1['returnMsg'];
                break;
            case 'edit_daojishi' :
                $this->save([
                    'shoukuan_time' => time() + 30 * 86400
                ], [
                    'order_no' => $this->param ['order_no']
                ]);
                break;
            case 'shoukuan_pay' :
                // 设置发票收款状态
                $invoicedb = new Invoicedb ();
                switch ($this->param ['shoukuan_pay']) {
                    case 1 :
                        $state = 2;
                        break;
                    case 2 :
                        $state = 4;
                        break;
                    case 3 :
                        $state = 3;
                        break;
                    case 4 :
                        $state = 1;
                        break;
                    case 5 :
                        $state = 5;
                        break;
                }
                if ($state != 5) {
                    $invoicedb->save([
                        'order_shoukuan' => $state
                    ], [
                        'order_nos' => $this->param ['order_no']
                    ]);
                    // 发送短信
                    $get_invoice_data = Db::table('__INVOICE__ i')->where('order_nos', $this->param ['order_no'])->where('order_shoukuan', 'NEQ', '1')->join('__ORDER__ o', 'o.order_no = i.order_nos')->join('__MEMBER__ m', 'm.member_id = o.supplier_id')->field('o.order_no,m.phone,o.order_money,o.out_order_no')->find();
                    if ($get_invoice_data) {
                        // $get_invoice_data = $get_invoice_data->toArray ();
                        $msg_json ['order'] = $get_invoice_data ['out_order_no'];
                        $msg_json ['money'] = $get_invoice_data ['order_money'] / 100;
                        $result = $this->sendMsg($get_invoice_data ['phone'], 'SMS_225910508', $msg_json);
                        // sendEmail($get_invoice_data ['phone'],'取消订单','订单号：'.$msg_json ['order'].'，订单金额：'.$msg_json ['money']);
                        $invoicedb->save([
                            'send_msg' => 1
                        ], [
                            'order_nos' => $this->param ['order_no']
                        ]);
                    }
                    $order_logdb = new OrderLogdb ();
                    $order_logdb->param = array(
                        'type_index' => 13,
                        'order_no' => $this->param ['order_no'],
                        'member_id' => MEMBER_ID,
                        'view_text' => '供货商收款(' . $this->param ['zhuangtai_text'] . ')，备注：' . $this->param ['shoukuan_intro'],
                        'post_json' => $this->param
                    );
                    $order_logdb->saveData();
                }
                $order_no = $this->param ['order_no'];
                unset ($this->param ['order_no']);
                $this->save($this->param, [
                    'order_no' => $order_no
                ]);
                break;
            case 're_ghs_zhiyi' :
                $order_no = $this->param ['order_no'];
                unset ($this->param ['order_no']);
                $this->save($this->param, [
                    'order_no' => $order_no
                ]);
                break;
            case 're_ghs_zhiyi' :
                $order_no = $this->param ['order_no'];
                unset ($this->param ['order_no']);
                $this->save($this->param, [
                    'order_no' => $order_no
                ]);
                break;
            case 're_shoukuan_zhiyi' :
                $order_no = $this->param ['order_no'];
                unset ($this->param ['order_no']);
                $this->save($this->param, [
                    'order_no' => $order_no
                ]);
                break;
            case 'shoukuan_zhiyi' :
                $order_no = $this->param ['order_no'];
                unset ($this->param ['order_no']);
                $this->save($this->param, [
                    'order_no' => $order_no
                ]);
                break;
            case 'ghs_pay' :
                // 设置发票付款状态
                $invoicedb = new Invoicedb ();
                switch ($this->param ['ghs_pay']) {
                    case 1 :
                        $state = 2;
                        break;
                    case 2 :
                        $state = 4;
                        break;
                    case 3 :
                        $state = 3;
                        break;
                    case 4 :
                        $state = 1;
                        break;
                }
                $invoicedb->save([
                    'order_fukuan' => $state
                ], [
                    'order_nos' => $this->param ['order_no']
                ]);
                // 发送短信
                $get_invoice_data = Db::table('__INVOICE__ i')->where('order_nos', $this->param ['order_no'])->where('order_shoukuan', 'NEQ', '1')->join('__ORDER__ o', 'o.order_no = i.order_nos')->join('__MEMBER__ m', 'm.member_id = o.supplier_id')->field('o.order_no,m.phone,o.order_money,o.out_order_no')->find();
                if ($get_invoice_data) {
                    // $get_invoice_data = $get_invoice_data->toArray ();
                    $msg_json ['order'] = $get_invoice_data ['out_order_no'];
                    $msg_json ['money'] = $get_invoice_data ['order_money'] / 100;
                    $result = $this->sendMsg($get_invoice_data ['phone'], 'SMS_225935234', $msg_json);
                }
                $order_logdb = new OrderLogdb ();
                $order_logdb->param = array(
                    'type_index' => 13,
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => '供货商付款(' . $this->param ['zhuangtai_text'] . ')，备注：' . $this->param ['ghs_intro'],
                    'post_json' => $this->param
                );
                $order_logdb->saveData();
                $order_no = $this->param ['order_no'];
                unset ($this->param ['order_no']);
                $this->save($this->param, [
                    'order_no' => $order_no
                ]);
                break;
            case 'caigou_hetong' :
                $order_logdb = new OrderLogdb ();
                $order_logdb->param = array(
                    'type_index' => 8,
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => '上传采购合同',
                    'post_json' => $this->param
                );
                $order_logdb->saveData();
                $order_no = $this->param ['order_no'];
                $this->param ['utec_upload_flag'] = 1;
                unset ($this->param ['order_no']);
                $this->save($this->param, [
                    'order_no' => $order_no
                ]);
                break;
            case 'caigou_invoice' :
                $invoice_title = '采购发票';
                $invoice_types = 1;
                if (isset($this->param['invoice_types'])) {
                    if ($this->param['invoice_types'] == 2) {
                        $invoice_title = '优士发票';
                    } elseif ($this->param['invoice_types'] == 3) {
                        $invoice_title = '定期发票';
                    }
                    $invoice_types = $this->param['invoice_types'];
                }
                $get_order_arr = explode(',', $this->param['order_no']);
                $duty_arr = [];
                if ($this->param ['caigou_state'] == 2) {
                    $pz = explode(',', $this->param['caigou_invoice_pz']);
                    $new_arr = array_values(array_filter($pz));
                    $this->param['caigou_invoice_pz'] = implode(',', $new_arr);
                    for ($i = 0; $i < count($new_arr); $i++) {
                        array_push($duty_arr, $this->param['caigou_invoice_duty']);
                    }
                }
                // 修改请款状态
                $kaipiao = 0;
                if (isset($this->param['caigou_invoice_pz']) && $this->param['caigou_invoice_pz']) {
                    $kaipiao = 1;
                }
                // 结束

                foreach ($get_order_arr as $key => $val) {
                    // 修改请款状态
                    $qingkuandb = new Qingkuandb();
                    $get_out_order = Db::name('order')->where('order_no', $val)->value('out_order_no');
                    $qingkuandb->where('out_order_no', $get_out_order)->update(['kaipiao' => $kaipiao]);
                    // 结束

                    $this->param['order_no'] = $val;
                    $order_logdb = new OrderLogdb ();
                    // 日志
                    if ($this->param ['caigou_state'] == 2) {
                        // 设置发票状态
                        $invoicedb = new Invoicedb ();
                        if ($invoice_types == 1) {
                            $invoicedb->save([
                                'gys_state' => 2
                            ], [
                                'order_nos' => $this->param ['order_no']
                            ]);
                        }
                        $order_logdb->param = array(
                            'type_index' => 6,
                            'order_no' => $this->param ['order_no'],
                            'member_id' => MEMBER_ID,
                            'view_text' => '上传' . $invoice_title,
                            'post_json' => $this->param
                        );
                    } elseif ($this->param ['caigou_state'] == 1) {
                        $order_logdb->param = array(
                            'type_index' => 7,
                            'order_no' => $this->param ['order_no'],
                            'member_id' => MEMBER_ID,
                            'view_text' => '审核通过' . $invoice_title,
                            'post_json' => $this->param
                        );
                        if ($invoice_types == 1) {
                            $UOrderdb = new UOrderdb();
                            $get_u_order = $UOrderdb->useGlobalScope(false)->where('order_no', $this->param['order_no'])->find();
                            if ($get_u_order) {
                                $get_u_order->save(
                                    [
                                        'ucaigou_state' => $this->param['caigou_state'],
                                    ]
                                );
                            }
                        }
                    } else {
                        if ($invoice_types == 1) {
                            $invoicedb = new Invoicedb ();
                            $invoicedb->save([
                                'gys_state' => 1
                            ], [
                                'order_nos' => $this->param ['order_no']
                            ]);
                        }
                        $order_logdb->param = array(
                            'type_index' => 101,
                            'order_no' => $this->param ['order_no'],
                            'member_id' => MEMBER_ID,
                            'view_text' => '驳回' . $invoice_title,
                            'post_json' => $this->param
                        );
                        if ($invoice_types == 1) {
                            $UOrderdb = new UOrderdb();
                            $get_u_order = $UOrderdb->useGlobalScope(false)->where('order_no', $this->param['order_no'])->find();
                            if ($get_u_order) {
                                $get_u_order->save(
                                    [
                                        'ucaigou_state' => $this->param['caigou_state'],
                                    ]
                                );
                            }
                        }
                    }
                    $order_logdb->saveData();
                    $order_no = $this->param ['order_no'];
                    unset ($this->param ['order_no']);
                    $orderdb = new Orderdb();
                    $get_order = $orderdb->where('order_no', $order_no)->find();
                    // 组合数据
                    if ($this->param ['caigou_state'] == 2) {
                        if ($invoice_types == 1) {
                            // 组合数据
                            $x_duty = explode(',', $get_order['caigou_invoice_duty']);
                            $arr_duty = array_values(array_filter(array_merge($x_duty, $duty_arr)));
                            $x_pz = explode(',', $get_order['caigou_invoice_pz']);
                            $arr_pz = array_values(array_filter(array_merge($x_pz, $new_arr)));
                            // 数据组合结束
                            $get_order->save([
                                'caigou_invoice_index' => $this->param['caigou_invoice_index'],
                                'caigou_state' => $this->param['caigou_state'],
                                'caigou_invoice_duty' => implode(',', $arr_duty),
                                'caigou_invoice_pz' => implode(',', $arr_pz),
                                'caigou_invoice_time' => time()
                            ]);

                            // 同步二级系统
                            $UOrderdb = new UOrderdb();
                            $get_u_order = $UOrderdb->useGlobalScope(false)->where('order_no', $order_no)->find();
                            if ($get_u_order) {
                                // 组合数据
                                $x_duty = explode(',', $get_u_order['caigou_invoice_duty']);
                                $arr_duty = array_values(array_filter(array_merge($x_duty, $duty_arr)));
                                $x_pz = explode(',', $get_u_order['caigou_invoice_pz']);
                                $arr_pz = array_values(array_filter(array_merge($x_pz, $new_arr)));
                                // 数据组合结束
                                $get_u_order->save([
                                    'ucaigou_invoice_duty' => implode(',', $arr_duty),
                                    'ucaigou_invoice_pz' => implode(',', $arr_pz),
                                    'ucaigou_invoice_time' => time(),
                                    'ucaigou_state' => $this->param['caigou_state'],
                                    'ucaigou_invoice_index' => $this->param['caigou_invoice_index']
                                ]);
                            }
                        } elseif ($invoice_types == 2) {
                            // 组合数据
                            $x_duty = explode(',', $get_order['utec_caigou_invoice_duty']);
                            $arr_duty = array_values(array_filter(array_merge($x_duty, $duty_arr)));
                            $x_pz = explode(',', $get_order['utec_caigou_invoice_pz']);
                            $arr_pz = array_values(array_filter(array_merge($x_pz, $new_arr)));
                            // 数据组合结束
                            $get_order->save([
                                'utec_invoice_index' => $this->param['caigou_invoice_index'],
                                'utec_invoice_state' => $this->param['caigou_state'],
                                'utec_caigou_invoice_duty' => implode(',', $arr_duty),
                                'utec_caigou_invoice_pz' => implode(',', $arr_pz)
                            ]);
                        } elseif ($invoice_types == 3) {
                            // 组合数据
                            $x_duty = explode(',', $get_order['dq_caigou_invoice_duty']);
                            $arr_duty = array_values(array_filter(array_merge($x_duty, $duty_arr)));
                            $x_pz = explode(',', $get_order['dq_caigou_invoice_pz']);
                            $arr_pz = array_values(array_filter(array_merge($x_pz, $new_arr)));
                            // 数据组合结束
                            $get_order->save([
                                'dq_invoice_index' => $this->param['caigou_invoice_index'],
                                'dq_invoice_state' => $this->param['caigou_state'],
                                'dq_caigou_invoice_duty' => implode(',', $arr_duty),
                                'dq_caigou_invoice_pz' => implode(',', $arr_pz)
                            ]);
                        }
                    } else {
                        if ($invoice_types == 1) {
                            $get_order->save([
                                'caigou_state' => $this->param['caigou_state']
                            ]);
                        } elseif ($invoice_types == 2) {
                            $get_order->save([
                                'utec_invoice_state' => $this->param['caigou_state']
                            ]);
                        } elseif ($invoice_types == 3) {
                            $get_order->save([
                                'dq_invoice_state' => $this->param['caigou_state']
                            ]);
                        }
                    }

                }
                break;
            case 'returned_refuse' :
                // 退换货
                $cartdb = new Cartdb ();
                $cartdb->param ['order_no'] = $this->param ['order_no'];
                $cartdb->param ['goods_sku'] = $this->param ['post_data'] ['goods_sku'];
                $cartdb->param ['returned_state'] = $this->param ['post_data'] ['returned_state'];
                $cartdb->setReturned();
                break;
            case 'edit_cart' :
                // 修改购物车信息
                $cartdb = new Cartdb ();
                $cartdb->param ['order_no'] = $this->param ['order_no'];
                $cartdb->param ['goods_sku'] = $this->param ['post_data'] ['goods_sku'];
                $cartdb->param ['post_data'] = $this->param ['post_data'];
                $order_money = $cartdb->editCart();
                // 如果是独立开票 修改发票价格
                $order_info = $this->ajaxFind();
                if ($order_info ['is_invoice'] == 2) {
                    $invoicedb = new Invoicedb ();
                    $invoicedb->param ['order_no'] = $this->param ['order_no'];
                    $invoice_info = $invoicedb->ajaxFind();
                    $invoicedb->param ['invoice_id'] = $invoice_info ['invoice_id'];
                    $invoicedb->param ['field'] = 'invoice_money';
                    $invoicedb->param ['value'] = $order_money;
                    // 修改金额
                    $invoicedb->ajaxSave();
                    $get_cart_list = $cartdb->ajaxList();
                    $invoicedb->param ['invoice_id'] = $invoice_info ['invoice_id'];
                    $invoicedb->param ['field'] = 'cart_ids';
                    $invoicedb->param ['value'] = $get_cart_list ['cart_ids'];
                    // 修改cart_ids
                    $invoicedb->ajaxSave();
                }
                // 修改订单价格
                $this->save([
                    'order_money' => $order_money
                ], [
                    'order_no' => $this->param ['order_no']
                ]);
                break;
            case 'edit_shopping' :
                $order_logdb = new OrderLogdb ();
                $get_orders = $this->where('order_no', $this->param['order_no'])->find();
                $get_order_api = json_decode($get_orders['api_param'], true);
                $before_data = $get_order_api['Cityname'] . $get_order_api['address'] . ' ' . $get_order_api['name'] . ' ' . $get_order_api['mobile'];
                $after_data = '';
                if ($get_orders['shopping_id']) {
                    $shoppingdb = new Shoppingdb();
                    $get_shopping = $shoppingdb->ajaxFind($get_orders['shopping_id']);
                    if ($get_shopping) {
                        $after_data = $get_shopping['province_name'] . '·' . $get_shopping['city_name'] . '·' . $get_shopping['area_name'] . $get_shopping['street'] . ' ' . $get_shopping['contacts'] . ' ' . $get_shopping['phone'];
                    }
                }
                // 日志
                $order_logdb->param = array(
                    'type_index' => 1000,
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => $before_data . ';改为:' . $after_data,
                    'post_json' => $this->param ['post_data']
                );
                $order_logdb->saveData();
                // 分系统日志
                $uorder_logdb = new UOrderLogdb();
                $uorder_logdb->save($order_logdb->param);
                // 重新生成验收单
                $this->save(['ysd' => '生成中'], [
                    'order_no' => $this->param ['order_no']
                ]);
                $temp1['order_no'] = $this->param['order_no'];
                $temp1['buildtpl_type'] = 'ysd';
                $temp1['state'] = 2;
                $buildtpldb = new Buildtpldb();
                $buildtpldb->save($temp1);
                break;
            case 'edit_delivery_date' :
                $this->save($this->param ['post_data'], [
                    'order_no' => $this->param ['order_no']
                ]);
                $order_logdb = new OrderLogdb ();
                // 日志
                $order_logdb->param = array(
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => '修改预计交货日期为（' . $this->param ['post_data'] ['delivery_date'] . '）',
                    'post_json' => $this->param ['post_data']
                );
                $order_logdb->saveData();
                break;
            case 'edit_invoice' :
                if ($this->param ['post_data'] ['field'] == 'huikuandanwei') {
                    $this->save([
                        'huikuandanwei' => $this->param ['post_data'] ['value']
                    ], [
                        'order_no' => $this->param ['order_no']
                    ]);
                } else {
                    $invoicedb = new Invoicedb ();
                    $invoicedb->param = $this->param ['post_data'];
                    $invoicedb->ajaxSave();
                    // 修改发票信息
                    $order_logdb = new OrderLogdb ();
                    // 日志
                    $order_logdb->param = array(
                        'order_no' => $this->param ['order_no'],
                        'member_id' => MEMBER_ID,
                        'view_text' => '修改发票信息',
                        'post_json' => $this->param ['post_data']
                    );
                    $order_logdb->saveData();
                }
                // 同步到分系统
                if (in_array($this->param['post_data']['field'], ['invoice_beizhu', 'fapiaohao', 'invoice_duty', 'invoice_title'])) {
                    $UOrderdb = new UOrderdb();
                    $get_u_order = $UOrderdb->where('order_no', $this->param['order_no'])->find();
                    if ($get_u_order) {
                        if ($this->param ['post_data'] ['field'] == 'huikuandanwei') {
                            $get_u_order->save(['huikuandanwei' => $this->param['post_data']['value']]);
                        } else {
                            $UInvoicedb = new UInvoicedb();
                            $get_u_invoice = $UInvoicedb->where('order_nos', $this->param['order_no'])->find();
                            if ($get_u_invoice) {
                                $get_u_invoice->save([$this->param['post_data']['field'] => $this->param['post_data']['value']]);
                                $UOrderLogdb = new UOrderLogdb();
                                // 日志
                                $save_log = array(
                                    'order_no' => $this->param ['order_no'],
                                    'member_id' => MEMBER_ID,
                                    'view_text' => '修改发票信息',
                                    'post_json' => $this->param ['post_data']
                                );
                                $UOrderLogdb->save($save_log);
                            }
                        }
                    }
                }
                break;
            case 'add_judge' :
                // 发表评论
                $cartdb = new Cartdb ();
                $cartdb->param ['field'] = 'judge_state';
                $cartdb->param ['cart_ids'] = $this->param ['cart_ids'];
                $cartdb->param ['order_no'] = $this->param ['order_no'];
                $cartdb->editCartState();
                if ($cartdb->search_count == 0) {
                    $this->save([
                        'judge_state' => 2
                    ], [
                        'order_no' => $this->param ['order_no']
                    ]);
                } else {
                    $this->save([
                        'judge_state' => 3
                    ], [
                        'order_no' => $this->param ['order_no']
                    ]);
                }
                // 修改发票信息
                $order_logdb = new OrderLogdb ();
                $order_logdb->param = array(
                    'order_no' => $this->param ['order_no'],
                    'member_id' => $this->param ['member_id'],
                    'view_text' => '发表评价',
                    'post_json' => json_encode($this->param)
                );
                $order_logdb->saveData();
                break;
            case 'make_invoice' :
                // 开票
                $cartdb = new Cartdb ();
                $cartdb->param ['field'] = 'invoice_state';
                $cartdb->param ['cart_ids'] = $this->param ['cart_ids'];
                $cartdb->param ['order_no'] = $this->param ['order_no'];
                $cartdb->editCartState();
                /*if ($cartdb->search_count == 0) {
                    $this->save([
                        'invoice_state' => 2
                    ], [
                        'order_no' => $this->param ['order_no']
                    ]);
                } else {
                    $this->save([
                        'invoice_state' => 3
                    ], [
                        'order_no' => $this->param ['order_no']
                    ]);
                }*/
                // 修改发票信息
                $order_logdb = new OrderLogdb ();
                $order_logdb->param = array(
                    'type_index' => 11,
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => '开具发票',
                    'post_json' => json_encode($this->param)
                );
                $order_logdb->saveData();
                if (SITE_ID == 117) {
                    $jun_api = new Api ();
                    $get_out_order = $this->useGlobalScope(false)->where('order_no', $this->param ['order_no'])->find()->toArray();
                    $result = $jun_api->sendInvoiceInfo($get_out_order['out_order_no']);

                }
                break;
            case 'edit_remark' :
                $this->save($this->param ['post_data'], [
                    'order_no' => $this->param ['order_no']
                ]);
                $UOrderdb = new UOrderdb();
                $UOrderdb->param = $this->param;
                $UOrderdb->editRemark();
                // 保存记录
                if (trim($this->param['post_data']['remark'])) {
                    $orderRemarkdb = new OrderRemarkdb();
                    $save_remark = [];
                    $save_remark['order_no'] = $this->param['order_no'];
                    $save_remark['remark_type'] = 1;
                    $get_member = Memberdb::where('member_id', MEMBER_ID)->field('username')->find();
                    $save_remark['username'] = $get_member['username'];
                    $save_remark['remark_content'] = trim($this->param['post_data']['remark']);
                    $orderRemarkdb->save($save_remark);
                }
                break;
            case 'edit_member_remark' :
                $this->save($this->param ['post_data'], [
                    'order_no' => $this->param ['order_no']
                ]);
                break;
            case 'edit_beizhu2':
                // 保存客服备注2
                $this->save($this->param ['post_data'], [
                    'order_no' => $this->param ['order_no']
                ]);
                $log_text = '客服备注2修改为:' . $this->param['post_data']['beizhu2'];
                $order_logdb = new OrderLogdb();
                $order_logdb->param = array(
                    'type_index' => 0,
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => $log_text,
                    'post_json' => $this->param ['post_data']
                );
                $order_logdb->saveData();
                // 重新生成验收单
                /*$this->save(['ysd' => '生成中'], [
                    'order_no' => $this->param ['order_no']
                ]);
                $temp1['order_no'] = $this->param['order_no'];
                $temp1['buildtpl_type'] = 'ysd';
                $temp1['state'] = 2;
                $buildtpldb = new Buildtpldb();
                $buildtpldb->save($temp1);*/
                // 同步分系统
                $UOrderdb = new UOrderdb();
                $get_order = $UOrderdb->where('order_no', $this->param['order_no'])->find();
                if ($get_order) {
                    $get_order->save($this->param['post_data']);
                    $uorder_logdb = new UOrderLogdb();
                    $save_data = array(
                        'type_index' => 0,
                        'order_no' => $this->param ['order_no'],
                        'member_id' => MEMBER_ID,
                        'view_text' => $log_text,
                        'post_json' => json_encode($this->param ['post_data'])
                    );
                    $uorder_logdb->save($save_data);
                }
                break;
            case 'edit_supplier_bz' :
                // 修改内容1
                $this->save($this->param ['post_data'], [
                    'order_no' => $this->param ['order_no']
                ]);
                // 保存记录
                if (trim($this->param['post_data']['suppler_bz'])) {
                    $orderRemarkdb = new OrderRemarkdb();
                    $save_remark = [];
                    $save_remark['order_no'] = $this->param['order_no'];
                    $save_remark['remark_type'] = 2;
                    $get_member = Memberdb::where('member_id', MEMBER_ID)->field('username')->find();
                    $save_remark['username'] = $get_member['username'];
                    $save_remark['remark_content'] = trim($this->param['post_data']['suppler_bz']);
                    $orderRemarkdb->save($save_remark);
                }
                break;
            case 'edit_supplier_bz2' :
                // 修改内容2
                $this->save($this->param ['post_data'], [
                    'order_no' => $this->param ['order_no']
                ]);
                break;
            case 'edit_supplier_bz3' :
                // 修改内容3
                $this->save($this->param ['post_data'], [
                    'order_no' => $this->param ['order_no']
                ]);
                break;
            case 'edit_order_type' :
                // 修改订单类型
                $save_data = [];
                $save_data['order_type'] = $this->param['post_data']['order_type'];
                $save_data['order_type_time'] = time();
                $this->save($save_data, [
                    'order_no' => $this->param ['order_no']
                ]);
                // 同步到分系统
                $uorderdb = new UOrderdb();
                $get_u_order = $uorderdb->where('order_no', $this->param['order_no'])->find();
                if ($get_u_order) {
                    $get_u_order->save($save_data);
                }
                // 日志
                $type_arr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];
                $type_index = 0;
                //  $log_text = '修改订单类型为:' . $type_arr[$this->param['post_data']['order_type'] - 1];
                $log_text = '' . $type_arr[$this->param['post_data']['order_type'] - 1];
                $order_logdb = new OrderLogdb();
                $order_logdb->param = array(
                    'type_index' => $type_index,
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => $log_text,
                    'post_json' => $this->param ['post_data']
                );
                $order_logdb->saveData();
                $uorder_logdb = new UOrderLogdb();
                $save_data = array(
                    'type_index' => $type_index,
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => $log_text,
                    'post_json' => json_encode($this->param ['post_data'])
                );
                $uorder_logdb->save($save_data);
                break;
            case 'edit_shouhou_type' :
                // 修改收货类型
                $this->save($this->param ['post_data'], [
                    'order_no' => $this->param ['order_no']
                ]);
                // 日志
                $type_index = 0;
                $type_arr = ['无需售后', '漏发货', '多发货', '错发', '换货', '退货', '维修调试', '安装'];
                $log_text = '修改售后类型为:' . $type_arr[$this->param['post_data']['shouhou_type'] - 1];
                $order_logdb = new OrderLogdb();
                $order_logdb->param = array(
                    'type_index' => $type_index,
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => $log_text,
                    'post_json' => $this->param ['post_data']
                );
                $order_logdb->saveData();
                $uorder_logdb = new UOrderLogdb();
                $save_data = array(
                    'type_index' => $type_index,
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => $log_text,
                    'post_json' => json_encode($this->param ['post_data'])
                );
                $uorder_logdb->save($save_data);
                break;
            case 'add_pay' :
                // 独立支付
                $this->save($this->param ['post_data'], [
                    'order_no' => $this->param ['order_no']
                ]);
                $order_info = $this->ajaxFind();
                $order_logdb = new OrderLogdb ();
                $order_logdb->param = array(
                    'type_index' => 12,
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => '订单已支付',
                    'post_json' => json_encode($this->param)
                );
                $order_logdb->saveData();
                // 设置购物车状态
                $cartdb = new Cartdb ();
                $cartdb->param ['field'] = 'pay_state';
                $cartdb->param ['cart_ids'] = $this->param ['cart_ids'];
                $cartdb->param ['order_no'] = $this->param ['order_no'];
                $cartdb->editCartState();
                // 写支付记录
                $payLogdb = new PayLogdb ();
                $payLogdb->param ['pay_order_no'] = $this->param ['order_no'];
                $payLogdb->param ['pay_option'] = json_encode($this->param ['post_data']);
                $payLogdb->param ['pay_money'] = $order_info ['order_money'];
                $payLogdb->param ['pay_cart_id'] = implode(',', $this->param ['cart_ids']);
                $payLogdb->param ['member_id'] = $order_info ['member_id'];
                $payLogdb->ajaxSave();
                break;
            case 'make_pay' :
                // 统一支付
                $cartdb = new Cartdb ();
                $cartdb->param ['field'] = 'pay_state';
                $cartdb->param ['cart_ids'] = $this->param ['cart_ids'];
                $cartdb->param ['order_no'] = $this->param ['order_no'];
                $cartdb->editCartState();
                $save_pay_data ['pay_no'] = $this->param ['pay_no'];
                $save_pay_data ['pay_time'] = time();
                $save_pay_data ['pay_bank'] = $this->param ['pay_bank'];
                $save_pay_data ['pay_card'] = $this->param ['pay_card'];
                $save_pay_data ['pay_card_name'] = $this->param ['pay_card_name'];
                $save_pay_data ['pay_state'] = $cartdb->search_count == 0 ? 2 : 3;
                $this->save($save_pay_data, [
                    'order_no' => $this->param ['order_no']
                ]);
                // 修改发票信息
                $order_logdb = new OrderLogdb ();
                $order_logdb->param = array(
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => '统一开票',
                    'post_json' => json_encode($this->param)
                );
                $order_logdb->saveData();
                break;
            case 'one_field' :
                $this->allowField(true)->save($this->param ['post_data'], [
                    'order_no' => $this->param ['order_no']
                ]);
                break;
            case 'edit_state' :
                $type_index = 0;
                $this->allowField(true)->save($this->param ['post_data'], [
                    'order_no' => $this->param ['order_no']
                ]);
                $order_logdb = new OrderLogdb ();
                switch ($this->param ['post_data'] ['order_state']) {
                    case 2 :
                        $this->allowField(true)->save([
                            'confirm_time' => time()
                        ], [
                            'order_no' => $this->param ['order_no']
                        ]);
                        // 确认订单 销量增加
                        /*
                         * $cartdb = new Cartdb();
                         * $cartdb->param['order_no'] = $this->param['order_no'];
                         * $cartdb->setGoodsSaleNum();
                         */
                        $log_text = '确认订单';
                        // 写推送信息
                        $postNoticedb = new PostNoticedb ();
                        $postNoticedb->param ['post_type'] = 28;
                        $postNoticedb->param ['sku_order_no'] = $this->param ['order_no'];
                        $postNoticedb->ajaxSave();
                        break;
                    case 3 :
                        $log_text = '订单发货';
                        $type_index = 9;
                        if ($this->param ['post_data'] ['count_state'] == 0) {
                            // 全部配送完成
                            $this->save([
                                'shopping_state' => 2,
                                'shopping_time' => time()
                            ], [
                                'order_no' => $this->param ['order_no']
                            ]);
                            $UOrderdb = new UOrderdb();
                            $get_u_order = $UOrderdb->where('order_no', $this->param['order_no'])->find();
                            if ($get_u_order) {
                                $get_u_order->save([
                                    'shopping_time' => time(),
                                    'shopping_state' => 2
                                ]);
                            }
                            // 全部发货 军采推送消息
                            if (SITE_ID == 117) {
                                $get_out_order = $this->useGlobalScope(false)->where('order_no', $this->param ['order_no'])->find()->toArray();
                                $jun_api = new Api ();
                                $expressdb = new Expressdb ();
                                $expressdb->param ['order_no'] = $this->param ['order_no'];
                                $get_list = $expressdb->where('order_no', $this->param ['order_no'])->select();
                                $post_json ['tradeNO'] = $get_out_order ['out_order_no'];
                                $post_json ['isAllSend'] = true;
                                $post_json ['packages'] = array();
                                $post_json['deliveryaddress'] = $get_out_order['gys_province'] . '-' . $get_out_order['gys_city'];
                                foreach ($get_list as $key => $val) {
                                    $temp ['packageNo'] = $val ['express_id'];
                                    $temp ['sku'] = '';
                                    $temp ['num'] = 0;
                                    $t = array();
                                    $cartdb = new Cartdb();
                                    $get_cart = $cartdb->whereIn('cart_id', explode(',', $val['cart_ids']))->select();
                                    foreach ($get_cart as $k => $v) {
                                        array_push($t, $v ['goods_sku']);
                                        $temp ['num'] += $v ['goods_num'];
                                    }
                                    $temp ['sku'] = implode(',', $t);
                                    $temp ['logisticsNo'] = ($val ['express'] == '自配物流' || $val ['express'] == 'ziji') ? '400-0888-037' : $val ['express_no'];
                                    $temp ['logisticsType'] = changeExpress($val ['express']);
                                    $temp ['estimateddeliverytime'] = $get_out_order ['delivery_date'];
                                    $temp ['deliveryaddress'] = $get_out_order['gys_province'] . '-' . $get_out_order['gys_city'];
                                    // halt($temp);
                                    array_push($post_json ['packages'], $temp);
                                }
                                $up_order_no = strtoupper($this->param ['order_no']);
                                if (strpos($up_order_no, 'X') === false && $get_out_order['yanchi'] != 'yes') {
                                    $result = $jun_api->ajaxLogisticPackageInfo($post_json);
                                    if ($result['isSuccess']) {
                                        $jun_api->sendOrderLogistics($get_out_order['out_order_no']);
                                    }
                                }

                            }
                        } else {
                            $this->save([
                                'shopping_state' => 3,
                                'shopping_time' => time()
                            ], [
                                'order_no' => $this->param ['order_no']
                            ]);
                        }
                        $postNoticedb = new PostNoticedb ();
                        $postNoticedb->param ['post_type'] = 12;
                        $postNoticedb->param ['sku_order_no'] = $this->param ['order_no'];
                        $postNoticedb->ajaxSave();
                        break;
                    case 4 :
                        $log_text = '订单收货';
                        $type_index = 10;
                        // 设置收货物流
                        if (!is_array($this->param ['post_data'] ['express_id'])) {
                            $express_ids = explode(',', $this->param ['post_data'] ['express_id']);
                        } else {
                            $express_ids = $this->param ['post_data'] ['express_id'];
                        }
                        for ($j = 0; $j < count($express_ids); $j++) {
                            $expressdb = new Expressdb ();
                            $expressdb->param ['express_id'] = $express_ids[$j];
                            $expressdb->param ['receipt_time'] = time();
                            $expressdb->param ['receipt_state'] = 2;
                            if (isset ($this->param ['post_data'] ['member_id']) && $this->param ['post_data'] ['member_id']) {
                                $receipt_member_id = $this->param ['post_data'] ['member_id'];
                            } else {
                                $receipt_member_id = MEMBER_ID;
                            }
                            $expressdb->param ['receipt_member_id'] = $receipt_member_id;
                            $expressdb->ajaxSave();
                            $get_express_info = $expressdb->ajaxFind();
                            // 设置购物车产品收货状态
                            $cartdb = new Cartdb ();
                            $cartdb->param ['order_no'] = $this->param ['order_no'];
                            $cartdb->param ['field'] = 'receipt_state';
                            $cartdb->param ['value'] = 1;
                            $cartdb->param ['cart_ids'] = explode(',', $get_express_info ['cart_ids']);
                            $cartdb->param ['is_exchange'] = $get_express_info ['is_exchange'];
                            $cartdb->editCartState();
                            if ($cartdb->search_count == 0) {
                                // 全部配送完成
                                $this->save([
                                    'receipt_state' => 2,
                                    'receipt_time' => time()
                                ], [
                                    'order_no' => $this->param ['order_no']
                                ]);
                            } else {
                                $this->save([
                                    'receipt_state' => 3,
                                    'receipt_time' => time()
                                ], [
                                    'order_no' => $this->param ['order_no']
                                ]);
                            }
                        }

                        // 军采推送消息
                        if (SITE_ID == 117) {

                            $jun_api = new Api ();
                            $get_out_order = $this->useGlobalScope(false)->where('order_no', $this->param ['order_no'])->find()->toArray();
                            $post_json ['tradeNo'] = $get_out_order ['out_order_no'];
                            $post_json ['packageNo'] = $this->param ['post_data'] ['express_id'];
                            $jun_api->sendOrderLogistics($get_out_order['out_order_no']);
                            $jun_api->ajaxPackageOverNotice($post_json);
                            //return '推送信息:状态1' . $result['returnMsg'] . ',状态2' . $result1['returnMsg'].',订单号:'.$this->param ['order_no'];
                        }
                        break;
                    case 5 :
                        $this->allowField(true)->save([
                            'over_time' => time()
                        ], [
                            'order_no' => $this->param ['order_no']
                        ]);
                        $postNoticedb = new PostNoticedb ();
                        $postNoticedb->param ['post_type'] = 5;
                        $postNoticedb->param ['sku_order_no'] = $this->param ['order_no'];
                        $postNoticedb->ajaxSave();
                        $log_text = '订单完成';
                        $type_index = 100;
                        break;
                    case -1 :
                        $type_index = 99;
                        $this->save([
                            'cancel_time' => time()
                        ], [
                            'order_no' => $this->param ['order_no']
                        ]);
                        $log_text = '订单取消';
                        $postNoticedb = new PostNoticedb ();
                        $postNoticedb->param ['post_type'] = 10;
                        $postNoticedb->param ['sku_order_no'] = $this->param ['order_no'];
                        $postNoticedb->ajaxSave();
                        $log_text = '订单完成';
                        break;
                }
                // 日志
                $order_logdb->param = array(
                    'type_index' => $type_index,
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => $log_text,
                    'post_json' => $this->param ['post_data']
                );
                $order_logdb->saveData();
                $uorder_logdb = new UOrderLogdb();
                $save_data = array(
                    'type_index' => $type_index,
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => $log_text,
                    'post_json' => json_encode($this->param ['post_data'])
                );
                $uorder_logdb->save($save_data);
                break;
        }
        Cache::clear('order');
        return 'success';
    }

    public function ajaxCount()
    {
        $count = $this->count();
        return $count;
    }

    // 添加配送
    public function addExpress()
    {
        //Db::startTrans();

        $cartdb = new Cartdb ();
        $cartdb->param ['order_no'] = $this->param ['order_no'];
        if (isset($this->param ['cart_goods_skus'])) {
            $cartdb->param ['goods_sku'] = explode(',', $this->param ['cart_goods_skus']);
        }
        if (isset ($this->param ['site_id'])) {
            $cartdb->param ['site_id'] = $this->param ['site_id'];
        }
        $cart_info = $cartdb->ajaxList();
        /*
         * foreach ($cart_info['list'] as $key => $val) {
         * if ($val['out_state'] == 2) {
         * return '商品：' . $val['goods_sku'] . '还没有出库';
         * }
         * }
         */
        $expressdb = new Expressdb ();
        $expressdb->param ['express'] = $this->param ['express'];
        $expressdb->param ['express_no'] = $this->param ['express_no'];
        // $expressdb->param['depot_id'] = $this->param['depot_id'];
        $expressdb->param ['order_no'] = $this->param ['order_no'];
        $expressdb->param ['cart_ids'] = $cart_info ['cart_id_arr'];
        $expressdb->ajaxSave();
        $get_express_id = $expressdb->lastId;
        // 修改购物车
        $cartdb->_emptyParam();
        $cartdb->param ['order_no'] = $this->param ['order_no'];
        $cartdb->param ['field'] = 'pack_id';
        $cartdb->param ['value'] = $get_express_id;
        // $cartdb->param['depot_id'] = $this->param['depot_id'];
        if (isset ($this->param ['site_id'])) {
            $cartdb->param ['site_id'] = $this->param ['site_id'];
        }
        $cartdb->param ['cart_ids'] = explode(',', $cart_info ['cart_id_arr']);
        $cartdb->editCartState();
        $count_state = $cartdb->search_count;
        // 更新订单状态
        $order_no = $this->param ['order_no'];
        $old_post = $this->param;
        $this->_emptyParam();
        $this->param ['event'] = 'edit_state';
        $this->param ['order_no'] = $order_no;
        $this->param ['post_data'] ['order_state'] = 3;
        $this->param ['post_data'] ['count_state'] = $count_state;
        $this->param ['post_data'] ['post_json'] = $old_post;
        $this->editOrder();
        //Db::commit();
        return 'success';
    }

    // 软删除
    public function ajaxDelete()
    {
        if (isset ($this->param ['order_id'])) {
            // 处理发送短信的通知
            $get_order = $this->useGlobalScope(false)->where('order_id', 'IN', $this->param ['order_id'])->select();
            if ($get_order) {
                $get_order = $get_order->toArray();
                $order_no_arr = [];
                foreach ($get_order as $key => $val) {
                    if ($val ['supplier_id']) {
                        // 有指派的库房
                        $memberdb = new Memberdb ();
                        $get_member = $memberdb->useGlobalScope(false)->where('member_id', $val ['supplier_id'])->find();
                        if ($get_member) {
                            $get_member = $get_member->toArray();
                            $post_param ['order'] = $val ['out_order_no'];
                            if ($get_member) {
                                $this->sendMsg($get_member ['phone'], 'SMS_229610853', $post_param);
                            }
                        }
                    }
                    array_push($order_no_arr, $val['order_no']);
                }
                // 删除chat
                Cartdb::whereIn('order_no', $order_no_arr)->delete();
                // 删除invoice
                Invoicedb::whereIn('order_nos', $order_no_arr)->delete();
                // 删除express
                Expressdb::whereIn('order_no', $order_no_arr)->delete();
            }
            // 处理通知完成
            if (isset ($this->param ['delete']) && $this->param ['delete'] == 'yes') {
                $this->destroy(function ($query) {
                    $query->where('order_id', 'IN', $this->param ['order_id']);
                }, true);
            } else {
                $this->destroy(function ($query) {
                    $query->where('order_id', 'IN', $this->param ['order_id']);
                }, true);
            }
            Cache::clear('order');
        }
    }

    public function getZhekouMoneyAttr($val, $row)
    {
        if ($row['supplier_rate']) {
            $zhekou_money = 0;
            $cartdb = new Cartdb ();
            $get_cart = $cartdb->useGlobalScope(false)->where('order_no', $row ['order_no'])->select();
            foreach ($get_cart as $s => $a) {
                $zhekou_money += round($a ['goods_price'] * $row['supplier_rate']) / 100 * $a ['goods_num'];
            }
            $supplier_rate_money = number_format($zhekou_money, 2, '.', '');

        } else {
            $supplier_rate_money = 0;
        }
        return $supplier_rate_money;
    }

    // 数据处理
    protected function handleData($val)
    {
        $cartdb = new Cartdb ();
        $shoppingdb = new Shoppingdb ();
        $uploaddb = new Uploaddb ();
        foreach ($val as $k => $v) {
            $val [$k] = $v;
            switch ($k) {
                case 'remark' :
                    $patten = array(
                        "\r\n",
                        "\n",
                        "\r"
                    );
                    $val ['remark'] = $str = str_replace($patten, "<br/>", $v);
                    break;
                case 'supplier_rate' :
                    if ($v) {
                        $zhekou_money = 0;
                        $cartdb = new Cartdb ();
                        $get_cart = $cartdb->useGlobalScope(false)->where('order_no', $val ['order_no'])->field('goods_price,goods_num')->select();
                        foreach ($get_cart as $s => $a) {
                            $zhekou_money += round($a ['goods_price'] * $v) / 100 * $a ['goods_num'];
                        }
                        $val ['supplier_rate_money'] = number_format($zhekou_money, 2, '.', '');
                        if ($val['order_no'] == 'CS20230514013029396002') {
                            $val ['supplier_rate_money'] = 1306.68;
                        }
                        $val ['order_money_rate'] = number_format($val ['order_money'] * $v / 100, 2, '.', '');
                    } else {
                        $val ['supplier_rate_money'] = 0;
                    }
                    break;
                case 'caigou_invoice_duty' :
                    if ($v) {
                        $temp = explode(',', $v);
                    }
                    break;
                case 'shoukuan_time' :
                    if ($v) {
                        $val ['shoukuan_time'] = ceil(($v - time()) / 86400) . '天';
                    }
                    break;
                case 'caigou_invoice_pz' :
                    if (isset($this->param['no_need_caigou_invoice_pz']) && $this->param['no_need_caigou_invoice_pz'] == 'yes') {
                        $val ['caigou_invoice_pz_info'] = [];
                    } else {
                        $val ['caigou_invoice_pz_info'] = array();
                        if ($v) {
                            $temp = explode(',', $v);
                            $temp1 = explode(',', $val ['caigou_invoice_duty']);
                            for ($i = 0; $i < count($temp); $i++) {
                                $files = $uploaddb->getFind($temp [$i]);
                                if ($files) {
                                    $t ['save_path'] = $files ['save_path'] ? $files ['save_path'] : '';
                                    $t ['file_name'] = isset($files ['file_name']) && $files ['file_name'] ? $files ['file_name'] : '';
                                    $t ['duty'] = $temp1 [$i];
                                    array_push($val ['caigou_invoice_pz_info'], $t);
                                }
                            }
                        }
                    }
                    break;
                case 'ucaigou_invoice_pz' :
                    if (isset($this->param['no_need_ucaigou_invoice_pz']) && $this->param['no_need_ucaigou_invoice_pz'] == 'yes') {
                        $val ['ucaigou_invoice_pz_info'] = [];
                    } else {
                        $val ['ucaigou_invoice_pz_info'] = array();
                        if ($v) {
                            $temp = explode(',', $v);
                            $temp1 = explode(',', $val ['ucaigou_invoice_duty']);
                            for ($i = 0; $i < count($temp); $i++) {
                                $files = $uploaddb->getFind($temp [$i]);
                                if ($files) {
                                    $t ['save_path'] = $files ['save_path'] ? $files ['save_path'] : '';
                                    $t ['file_name'] = isset($files ['file_name']) && $files ['file_name'] ? $files ['file_name'] : '';
                                    $t ['duty'] = $temp1 [$i];
                                    array_push($val ['ucaigou_invoice_pz_info'], $t);
                                }
                            }
                        }
                    }
                    break;
                case 'utec_caigou_invoice_pz' :
                    if (isset($this->param['no_need_caigou_invoice_pz']) && $this->param['no_need_caigou_invoice_pz'] == 'yes') {
                        $val ['utec_caigou_invoice_pz_info'] = [];
                    } else {
                        $val ['utec_caigou_invoice_pz_info'] = array();
                        if ($v) {
                            $temp = explode(',', $v);
                            $temp1 = explode(',', $val['utec_caigou_invoice_duty']);
                            for ($i = 0; $i < count($temp); $i++) {
                                $files = $uploaddb->getFind($temp [$i]);
                                if ($files) {
                                    $t ['save_path'] = $files ['save_path'] ? $files ['save_path'] : '';
                                    $t ['file_name'] = isset($files ['file_name']) && $files ['file_name'] ? $files ['file_name'] : '';
                                    $t ['duty'] = $temp1 [$i];
                                    array_push($val ['utec_caigou_invoice_pz_info'], $t);
                                }
                            }
                        }
                    }
                    break;
                case 'dq_caigou_invoice_pz' :
                    if (isset($this->param['no_need_caigou_invoice_pz']) && $this->param['no_need_caigou_invoice_pz'] == 'yes') {
                        $val ['dq_caigou_invoice_pz_info'] = [];
                    } else {
                        $val ['dq_caigou_invoice_pz_info'] = array();
                        if ($v) {
                            $temp = explode(',', $v);
                            $temp1 = explode(',', $val['dq_caigou_invoice_duty']);
                            for ($i = 0; $i < count($temp); $i++) {
                                $files = $uploaddb->getFind($temp [$i]);
                                if ($files) {
                                    $t ['save_path'] = $files ['save_path'] ? $files ['save_path'] : '';
                                    $t ['file_name'] = isset($files ['file_name']) && $files ['file_name'] ? $files ['file_name'] : '';
                                    $t ['duty'] = $temp1 [$i];
                                    array_push($val ['dq_caigou_invoice_pz_info'], $t);
                                }
                            }
                        }
                    }
                    break;
                case 'caigou_hetong' :
                    if ($v) {
                        $val ['caigou_hetong_info'] = $uploaddb->getFind($v);
                    }
                    break;
                case 'hetong_id' :
                    if ($v) {
                        $val ['hetong'] = $uploaddb->getFind($v);
                    }
                    break;
                case 'yanshoudan_id' :
                    if ($v) {
                        $val ['yanshoudan'] = $uploaddb->getFind($v);
                    }
                    break;
                case 'member_id' :
                    $memberdb = new Memberdb ();
                    $val ['member_info'] = $memberdb->memberInfo($val ['member_id']);
                    break;
                case 'order_money' :
                    $val ['order_money'] = number_format($val ['order_money'] / 100, 2, '.', '');
                    break;
                case 'shopping_id' :
                    $shoppingdb = new Shoppingdb ();
                    $val ['shopping_info'] = $shoppingdb->ajaxFind($val ['shopping_id']);
                    break;
                case 'pay_type' :
                    switch ($v) {
                        case '1' :
                            $val ['pay_type_text'] = '货到付款';
                            break;
                        case '2' :
                            $val ['pay_type_text'] = '在线支付';
                            break;
                        case '3' :
                            $val ['pay_type_text'] = '账期支付';
                            break;
                        case '4' :
                            $val ['pay_type_text'] = '银行转账';
                            break;
                        default :
                            $val ['pay_type_text'] = '货到付款';
                            break;
                    }
                    break;
                case 'pay_state' :
                    switch ($v) {
                        case '1' :
                            $val ['pay_state_text'] = '未支付';
                            break;
                        case '2' :
                            $val ['pay_state_text'] = '全部收款';
                            break;
                        case '3' :
                            $val ['pay_state_text'] = '部分支付';
                            break;
                        case '4' :
                            $val ['pay_state_text'] = '质保金支付';
                            break;
                    }
                    if (isset ($this->param ['need_pay_info']) && $this->param ['need_pay_info'] == 'yes') {
                        $paylogdb = new PayLogdb ();
                        $paylogdb->param ['order_no'] = $val ['order_no'];
                        $paylogdb->param ['no_need_pay_member_info'] = 'yes';
                        $paylogdb->param ['page'] = 1;
                        $paylogdb->param ['limit'] = 1000;
                        $get_paylog = $paylogdb->ajaxList();
                        $val ['payinfo'] = $get_paylog;
                    }
                    break;
                case 'invoice_state' :
                    switch ($v) {
                        case '1' :
                            $val ['invoice_state_text'] = '未开票';
                            break;
                        case '2' :
                            $val ['invoice_state_text'] = '已开票';
                            break;
                        case '3' :
                            $val ['invoice_state_text'] = '部分开票';
                            break;
                    }
                    break;
                case 'order_state' :
                    switch ($v) {
                        case '1' :
                            $val ['order_state_text'] = '待确认';
                            break;
                        case '2' :
                            $val ['order_state_text'] = '待发货';
                            break;
                        case '3' :
                            $val ['order_state_text'] = '待收货';
                            break;
                        case '4' :
                            $val ['order_state_text'] = '待评价';
                            break;
                        case '5' :
                            $val ['order_state_text'] = '完成';
                            break;
                        case '-1' :
                            $val ['order_state_text'] = '已取消';
                            break;
                    }
                    break;
                case 'supplier_id' :
                    if (isset ($this->param ['need_supplier']) && $this->param ['need_supplier'] == 'yes') {
                        if ($v) {
                            $memberdb = new Memberdb ();
                            $get_supplier = $memberdb->where('member_id', $v)->find();
                            if ($get_supplier) {
                                $val ['supplier_name'] = $get_supplier ['supplier_name'];
                                $val ['yewuyuan'] = $get_supplier ['yewuyuan'];
                                $val ['zhuli'] = $get_supplier ['zhuli'];
                                $val ['neiqin'] = $get_supplier ['neiqin'];
                                $val ['chuliren'] = $get_supplier ['chuliren'];
                                $val ['supplier_type'] = $get_supplier['supplier_type'];
                            } else {
                                $val ['supplier_name'] = '未指定库房';
                                $val ['yewuyuan'] = '';
                                $val ['zhuli'] = '';
                                $val ['neiqin'] = '';
                                $val ['chuliren'] = '';
                                $val ['supplier_type'] = '';
                            }

                        } else {
                            $val ['supplier_name'] = '未指定库房';
                            $val ['yewuyuan'] = '';
                            $val ['zhuli'] = '';
                            $val ['neiqin'] = '';
                            $val ['chuliren'] = '';
                            $val ['supplier_type'] = '';
                        }
                    }
                    break;
                case 'supplier_er' :
                    if (isset ($this->param ['need_supplier']) && $this->param ['need_supplier'] == 'yes') {
                        if ($v) {
                            $memberdb = new Memberdb ();
                            $get_supplier = $memberdb->where('member_id', $v)->find();
                            if ($get_supplier) {
                                $val ['supplier_name2'] = $get_supplier ['supplier_name'];
                                $val ['yewuyuan2'] = $get_supplier ['yewuyuan'];
                                $val ['supplier_type2'] = $get_supplier['supplier_type'];
                            } else {
                                $val ['supplier_name2'] = '未指定库房';
                                $val ['yewuyuan2'] = '';
                                $val ['supplier_type2'] = '';
                            }
                        } else {
                            $val ['supplier_name2'] = '未指定库房';
                            $val ['yewuyuan2'] = '';
                            $val ['supplier_type2'] = '';
                        }
                    }
                    break;
                case 'is_invoice' :
                    if (!isset ($this->param ['no_need_invoice'])) {
                        $invoicedb = new Invoicedb ();
                        $invoicedb->param ['order_no'] = $val ['order_no'];
                        $invoicedb->param ['site_id'] = -1;
                        $invoicedb->param ['state'] = array(
                            '1',
                            '2',
                            '3',
                            '4'
                        );
                        if ($val ['invoice_state'] == 1) {
                            $val ['invoice_info'] [0] = $invoicedb->ajaxFind();
                            $val ['apply_state'] = isset ($val ['invoice_info'] ['apply_state']) ? $val ['invoice_info'] ['apply_state'] : 2;
                            if ($val ['apply_state'] == 1) {
                                $temp = $val ['invoice_info'];
                                $val ['invoice_info'] = array();
                                array_push($val ['invoice_info'], $temp);
                            }

                        } else {
                            $val ['invoice_info'] = $invoicedb->ajaxSelect();
                            $val ['apply_state'] = 1;
                        }
                        $val ['invoice_info_one'] = $val ['invoice_info'] [0];
                        $val ['invoice_info_json'] = json_encode($val ['invoice_info'] [0]);
                        $val ['invoice_proof'] = isset ($val ['invoice_info'] [0] ['invoice_proof']) && $val ['invoice_info'] [0] ['invoice_proof'] ? 1 : 2;
                        switch ($v) {
                            case '1' :
                                $val ['is_invoice_text'] = '不开票';
                                break;
                            case '2' :
                                $val ['is_invoice_text'] = '独立开票';
                                break;
                            case '3' :
                                $val ['is_invoice_text'] = '统一开票';
                                break;
                        }
                    }
                    break;
                case 'api_param' :
                    $temp = json_decode($v, true);
                    $val ['citys'] = $temp ['Cityname'] ?? '';
                    $val ['address'] = $temp ['address'] ?? '';
                    $val ['api_remark'] = isset($temp ['remark']) ? $temp ['remark'] : '';
                    $val ['payment'] = isset($temp ['payment']) && $temp ['payment'] == '9' ? '延期支付' : '货到付款';
                    $val ['email'] = $temp['email'] ?? '';
                    break;
                default :
                    break;
            }
        }
        if (isset($this->param['need_cart']) && $this->param['need_cart']) {
            $cartdb->param ['order_no'] = $val ['order_no'];
            if (isset ($this->param ['site_id'])) {
                $cartdb->param ['site_id'] = $this->param ['site_id'];
            }
            if (isset ($this->param ['need_goods_info']) && $this->param ['need_goods_info'] == 'yes') {
                $cartdb->param ['need_goods_info'] = 'yes';
            }
            if (isset ($this->param ['no_need_attr']) && $this->param ['no_need_attr'] == 'yes') {
                $cartdb->param ['no_need_attr'] = 'yes';
            }
            if (isset ($this->param ['no_need_goods_handle']) && $this->param ['no_need_goods_handle'] == 'yes') {
                $cartdb->param ['no_need_goods_handle'] = 'yes';
            }
            if (isset ($this->param ['no_need_returned']) && $this->param ['no_need_returned'] == 'yes') {
                $cartdb->param ['no_need_returned'] = 'yes';
            }
            if (isset ($this->param ['no_goods_picture'])) {
                $cartdb->param ['no_goods_picture'] = 'yes';
            }
            if (isset ($this->param ['no_more_price'])) {
                $cartdb->param ['no_more_price'] = 'yes';
            }
            $val ['cart_info'] = $cartdb->ajaxList();
        }
        if (isset ($this->param ['need_express_info']) && $this->param ['need_express_info'] == 'yes') {
            $expressdb = new Expressdb ();
            $expressdb->param ['order_no'] = $val ['order_no'];
            $val ['express_info'] = $expressdb->ajaxList();
        }
        // 配送地址
        if ($val ['shopping_id']) {
            $shoppingdb->param ['shopping_id'] = $val ['shopping_id'];
            $get_shopping_info = $shoppingdb->ajaxFind($val ['shopping_id']);
            $val ['contacts'] = $get_shopping_info ['contacts'];
            $val ['contacts_phone'] = $get_shopping_info ['phone'];
            $val ['shopping_info'] = $get_shopping_info;
        }
        if (isset ($this->param ['is_feizhi']) && $this->param ['is_feizhi'] == 'yes') {
            $val ['order_money'] = -$val ['order_money'];
            $val ['supplier_rate_money'] = -$val ['supplier_rate_money'];
        }
        return $val;
    }

    public function invoice()
    {
        return $this->hasOne('Invoicedb', 'order_nos', 'order_no')->bind([
            's_invoice_type' => 'invoice_type',
            's_invoice_title' => 'invoice_title',
            's_invoice_duty' => 'invoice_duty',
            's_invoice_addr' => 'invoice_addr',
            's_invoice_tel' => 'invoice_tel',
            's_invoice_bank' => 'invoice_bank',
            's_invoice_account' => 'invoice_account',
            's_invoice_beizhu' => 'invoice_beizhu',
            's_invoice_proof' => 'invoice_proof',
            's_invoice_proofs' => 'invoice_proofs',
            's_api_type' => 'api_type',
            's_invoice_api_bianhao' => 'api_bianhao'
        ]);
    }

    public function supplier()
    {
        return $this->belongsTo('Memberdb', 'supplier_id', 'member_id')->bind([
            'm_supplier_name' => 'supplier_name'
        ]);
    }

    public function applyAuditAdmin()
    {
        return $this->belongsTo('Memberdb', 'apply_audit_admin_id', 'member_id')->bind([
            'm_useranme' => 'username'
        ]);
    }

    public function carts()
    {
        return $this->hasMany('Cartdb', 'order_no', 'order_no');
    }

    public function getOrderMoneysAttr($value, $data)
    {
        return $data['order_money'] / 100;
    }
}
